4

I want to generate woocommerce coupon code dynamically.

My requirements is that after complete the order automatically generate one coupon code in admin side woocommerce coupon code list for particular product.

So any one know my above requirement solutions then please help me.

Thanks, Ketan.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Ketan
  • 579
  • 3
  • 11
  • 31

2 Answers2

4

You can used woocommerce_order_status_completed action hook for order complete. and create post with post type shop_coupon for coupan using wp_insert_post. check below code

function action_woocommerce_order_status_completed( $order_id ) { 

   $order = wc_get_order( $order_id );

    $order_items = $order->get_items();
    // Iterating through each item in the order
    $item_quantity=0;
    foreach ($order_items as $item_id => $item_data) {

        $item_quantity=$order->get_item_meta($item_id, '_qty', true);
        if($item_quantity>1){
            $product_ids[]=$item_data['product_id'];
            $coupon_code = 'UNIQUECODE'.$order_id.$item_id; // Code
            $amount = '10'; // Amount
            $discount_type = 'fixed_cart'; // 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',$product_ids );
            update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
            update_post_meta( $new_coupon_id, 'usage_limit', '' );
            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' );
            unset($product_ids);
        }

    }



}; 
// add the action 
add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 1 );
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
  • Thanks Ankur, For your replay. I will try your solutions and update you. Thanks. – Ketan Oct 12 '17 at 10:56
  • Hi Ankur, After adding above code in my functions.php file, site not working display blank white screen on both front end and back end side. So can you please review your above code and update me here. Thanks. – Ketan Oct 12 '17 at 11:05
  • $coupon_code = 'UNIQUECODE'$order_id; error in this line. please add `.` between UNIQUECODE and $order_id. Please check edited answer – Ankur Bhadania Oct 12 '17 at 11:06
  • Thanks Ankur, I do that and coupon code is generated successfully. One more think How we can only allow to generate that coupon code If customer purchased with more than one quantity products? Thanks. – Ketan Oct 12 '17 at 11:29
  • @KetanPatel check answer now – Ankur Bhadania Oct 12 '17 at 11:46
  • I have added your edited code but that code work same as old code. That code generate coupon code if product qty=1. I only want to generate coupon code if product qty is 2 OR more qty not only 1 qty. If product qty is 1 then can't allow to generate that code. Thanks. – Ketan Oct 12 '17 at 12:05
  • Now above code works good for me. That generate coupon code dynamically if order product qty is more then 1. Thanks for this. Actually now i want to generate coupon code dynamically per order products that qty is more then 1. Example: If in order: ABC product -> 1qty XYZ product -> 5qty PQR product - 3 qty Then in admin side after complete order generate two dynamic coupon code for XYZ product and PQR product. This coupon only use for for that each product. So please if you know then provide me this solutions. Thanks. :) – Ketan Oct 13 '17 at 04:34
  • Please accept and up vote answer if helpful.give me some time i will update you – Ankur Bhadania Oct 13 '17 at 04:39
  • Sure. Thanks. I am waiting for your replay. – Ketan Oct 13 '17 at 04:43
  • Thanks. I have tested above code and that works. I have added 3 products in cart and for two product i have selected qty more then 1. So after complete the order that generate two coupon code. But one missing thing is... Right now for that code no any product usage restriction. Example: ABC product -> 1qty, XYZ product -> 5qty, PQR product - 3 qty then XYZ product dynamically generate coupon code only apply for(Usage restriction) XYZ product not any other products. Same thinks for PQR products. So please review this Usage restriction condition and update me. Thanks. – Ketan Oct 13 '17 at 05:39
  • @KetanPatel XYZ product dynamically generate coupon code only apply to this product ? and any other restriction ? – Ankur Bhadania Oct 13 '17 at 06:06
  • For this please review following two screen shot: 1. http://nimb.ws/7pBjOv 2. http://nimb.ws/BSjftY – Ketan Oct 13 '17 at 06:21
  • @KetanPatel i have already do this `Check update_post_meta( $new_coupon_id, 'product_ids',$product_ids ) );` check in code. debug code or create one coupon form backend and check which value store in database and compere with our coupon. right now i am not able to test because i have not complete project setup, so please do your self. – Ankur Bhadania Oct 13 '17 at 06:31
  • Hi @Ankur, In your above code in place of $product_ids[] = $item_id; code i have added $product_id = $item_data['product_id']; code and my issue is fixed. Thanks for your good support. – Ketan Oct 13 '17 at 09:31
  • @KetanPatel why not up accept my answer. any reason for this. – Ankur Bhadania Oct 13 '17 at 09:45
  • Hi @ Ankur Bhadania, I have accepted your answer already. You're rock star man. Thanks :) – Ketan Oct 13 '17 at 09:48
  • Hi @Ankur, One last requirement here is, I want to set "Usage limit per coupon" for that dynamically generated coupon code. Here i want to set following condition: If Particular order product qty is 10 then for that product dynamically generated coupon code "Usage limit per coupon" is 9.(Means 10qty-1= 9). Same for if order product qty is 15 then for that product dynamically generated coupon code "Usage limit per coupon" is 14.(Means 15qty-1= 14). So please help me here "update_post_meta( $new_coupon_id, 'usage_limit', '' );" how we can set that condition. Thanks. – Ketan Oct 13 '17 at 11:00
  • @KetanPatel set your limit in `usage_limit` update_post_meta( $new_coupon_id, 'usage_limit', ($item_quantity-1) ); – Ankur Bhadania Oct 13 '17 at 11:05
0

Write a function which will fire when an order has been completed or placed using the woocommerce_order_status_completed or woocommerce_order_status_processing hook. Inside the function, use wp_insert_post to create a post of type shop_coupon. Using wp_insert_post, you can specify the title of the coupon, amount etc.

Neil K
  • 1,318
  • 1
  • 14
  • 23