php - How to check which coupon is applied to which product in WooCommerce? -


since can apply different coupons each product in order, there method know coupon applied product? i've used $order->get_used_coupons() function returns used coupon codes only.

please solution. thanks.

this based on compiled searches around solving issue, unsure (untested) snippet code:

function wc_my_order_coupons( $order_id ) {     $order = new wc_order( $order_id );      // checking if order has used coupons     if( $order->get_used_coupons() ) {         $order_coupons = $order->get_used_coupons();         /* echo var_dump($order_coupons); */          foreach( $order->$order_coupons $coupon) {             $coupon_id = $coupon->id;             /* echo var_dump($coupon_id); */              // here list of data can use coupon             $cp_discount_type = get_post_meta( $coupon_id, 'discount_type', true );             $cp_amount = get_post_meta( $coupon_id, 'coupon_amount', true );             $cp_indiv_use = get_post_meta( $coupon_id, 'individual_use', true );             $cp_products = get_post_meta( $coupon_id, 'product_ids' );              $cp_excl_products = get_post_meta( $coupon_id, 'exclude_product_ids' );             $cp_usage_limit = get_post_meta( $coupon_id, 'usage_limit', true );             $cp_expiry_date = get_post_meta( $coupon_id, 'expiry_date', true );             $cp_apply_before_tax = get_post_meta( $coupon_id, 'apply_before_tax', true );             $cp_free_shipping = get_post_meta( $coupon_id, 'free_shipping', true );               // getting products in order_id             $items = $order->get_items();             foreach ($items $key => $item) {                 $product_name = $item['name'];                 $product_id = $item['product_id'];                 /* echo var_dump($product_id); */                  // example: use product_ids authorized in coupon $cp_products                 if (in_array($product_id, $cp_products)) {                     echo $product_name . 'uses coupon: ' . $coupon->code . '<br>';                 }             }         }     } } 

give me feed back… , tune code, because there multiple way it.

references :


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -