How do I programmatically apply a coupon in Woocommerce?

First, create a discount coupon (via http://docs.woothemes.com/document/create-a-coupon-programatically/):

$coupon_code = 'UNIQUECODE'; // Code - perhaps generate this from the user ID + the order ID
$amount = '10'; // Amount
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product
$coupon = array(
    'post_title' => $coupon_code,
    'post_content' => '',
    'post_status' => 'publish',
    'post_author' => 1,
    'post_type'     => 'shop_coupon'
);   
$new_coupon_id = wp_insert_post( $coupon );
// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

Then apply that coupon to your order:

if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code )))
    $woocommerce->show_messages();

That last function returns a BOOL value: TRUE if the discount was successful, FALSE if it fails for any one of a variety of reasons.

How to get All Categories , Sub-Categories and Products in wooCommerce WordPress plugin

<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>

<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{

if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo "<ul class='category'><li>".$cat->name;
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty

);

$sub_cats = get_categories( $args2 );
if($sub_cats)
{

foreach($sub_cats as $sub_category)
{
echo "<ul class='subcategory'>";
if($sub_cats->$sub_category == 0)
{
echo "<li>".$sub_category->cat_name;

?>
<?php
/*echo "<pre>";
print_r($sub_category);
echo "</pre>";*/

$args = array( 'post_type' => 'product','product_cat' => $sub_category->slug);
$loop = new WP_Query( $args );
echo "<ul class='products'>";
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>
<?php wp_reset_query(); ?>

<?php
}
else
{
echo "</li></ul></li>";
}
echo "</ul>";
}
}
else
{
$args = array( 'post_type' => 'product', 'product_cat' => $cat->slug );
$loop = new WP_Query( $args );
echo "<ul class='products'>";
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul></li></ul>
<?php wp_reset_query();
}
}
else
{
echo "</li></ul>";
}
}
?>

How to split the categories of subcategories with thumbnails

how to split the categories of subcategories, while having only thumbnails subcategories eg. like this:

  • category (text)
    • subcategory (thumbnails)
    • subcategory (thumbnails)
  • category (text)
    • subcategory (thumbnails)
    • subcategory (thumbnails)

I found how to make this:

<?php
$taxonomy     = 'product_cat';
$orderby      = 'name';
$show_count   = 0;      // 1 for yes, 0 for no
$pad_counts   = 0;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no
$title        = '';
$empty        = 0;

$args = array(
  'taxonomy'     => $taxonomy,
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title,
  'hide_empty'   => $empty
);
?>

<?php $all_categories = get_categories( $args );

foreach ($all_categories as $cat) {  

    if($cat->category_parent == 0) {?>

<?php   $category_id = $cat->term_id;
        $thumbnail_id   = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image = wp_get_attachment_url( $thumbnail_id );
        echo '<h3 style="font-size:16px">'. $cat->name .'</h3>';

        $args2 = array(
          'taxonomy'     => $taxonomy,
          'child_of'     => 0,
          'parent'       => $category_id,
          'orderby'      => $orderby,
          'show_count'   => $show_count,
          'pad_counts'   => $pad_counts,
          'hierarchical' => $hierarchical,
          'title_li'     => $title,
          'hide_empty'   => $empty

        );

        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
        if($sub_cats->$sub_category == 0) {
            $thumbnail_id   = get_woocommerce_term_meta( $sub_category->term_id, 'thumbnail_id', true );
            $image = wp_get_attachment_url( $thumbnail_id );

            echo '<li style="list-style-type: none"><a href="'. get_term_link($sub_category->slug, 'product_cat') .'"><img src="'.$image.'" width="45px" height="45px" style="margin-right:8px; margin-top:8px; float:left; " /></a></li>';
            }
        }
        echo '</li>';
        }?>

<?php   } 
 } ?>