-1

I have Created a coupon programmatically using this documentation -> https://docs.woocommerce.com/document/create-a-coupon-programatically/

It's work good and generate one dynamic coupon code after complete the order. But i want to allow that coupon code only for that ordered product.

So on above code here:

update_post_meta( $new_coupon_id, 'product_ids', '' );

i want to get that order product ID using that order item id.

So any one know solutions for this then please help me.

Thanks, Ketan.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Ketan
  • 579
  • 3
  • 11
  • 31
  • Is this coupon for one product or multiple? – Sajjad Hossain Sagor Oct 13 '17 at 07:31
  • See [this link](http://www.webhat.in/article/woocommerce-tutorial/how-to-get-order-details-by-order-id/) you can use `$order->get_items()` to loop it and create an array of product ID. – Raunak Gupta Oct 13 '17 at 07:31
  • @Sajjadur Rahman Sagor, Only for that order products only. not for multiple products. For more information please review this screen shot -> http://nimb.ws/ly9NaZ Thanks. – Ketan Oct 13 '17 at 07:34
  • @Raunak Gupta, Can you please review this https://stackoverflow.com/questions/46707433/how-to-generate-woocommerce-coupon-code-dynamically-using-programming-code/46707664#46707664 and provide me solutions for my above requirements. thanks. – Ketan Oct 13 '17 at 07:42
  • @KetanPatel:if there are n number of product an order then you want to generate n number of coupon? – Raunak Gupta Oct 13 '17 at 08:21
  • @Raunak Gupta, Yes that code works good. Now only remaining that each product coupon code only apply for that product only. So if you know solutions then inform me. Thanks. – Ketan Oct 13 '17 at 08:37

1 Answers1

6

You can get product_id by using $order->get_items() function in WooCommerce

$order = new WC_Order( $order_id );
    $items = $order->get_items();
    foreach ( $items as $item ) {
        $item_id = $item['order_item_id']; 
        $product_name = $item['name'];
        $product_id = $item['product_id'];
    }
Shital Marakana
  • 2,817
  • 1
  • 9
  • 12