3

Since we can apply different coupons to each product in an order, Is there any method to know which coupon is applied to which product? I've used $order->get_used_coupons() function but it just returns the used coupon codes only.

Please help with a solution. Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Mûhámmàd Yäsår K
  • 1,492
  • 11
  • 24

1 Answers1

4

Example made for Woocommerce 2.5.x on June 2016 (Will not work in Woocommerce 3+)

This example is based on compiled searches:

function wc_my_order_coupons( $order_id ) {
    $order = new WC_Order( $order_id );

    // checking if order has some used coupons
    if( sizeof($order->get_used_coupons()) > 0 ) {
        foreach( $order->get_used_coupons() as $coupon_code ) {

            // Retrieving the coupon ID
            $post_obj = get_page_by_title($coupon_code, OBJECT, 'shop_coupon');
            $coupon_id = $post_obj->ID;

            // here is a list of most all data that you can use for a 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 the products in this order_id
            $items = $order->get_items();
            foreach ($items as $key => $item) {
                $product_name = $item['name'];
                $product_id = $item['product_id'];

                // Example: we use product_ids authorized in the coupon $cp_products
                if (in_array($product_id, $cp_products)) {
                    echo $product_name . 'uses the coupon: ' . $coupon->code . '<br>';
                }
            }
        }
    }
}

References :

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thaks for the snippet @LoicTheAztec, really appreciated. Sorry for the delay in response. I've used this code, but there will be an issue if a coupon can be applied to two or more products,am I correct? – Mûhámmàd Yäsår K Jun 28 '16 at 03:54
  • @MûhámmàdYäsårK Yes that could be right. But it could be solved certainly, because anything is possible depending on your time and effort. Thanks for up voting me :) … Is this snippet working or not? – LoicTheAztec Jun 28 '16 at 04:23
  • get_used_coupons() returns an array of coupon codes it's not an object as the above snippet is treating it as. – Andrew Schultz Oct 17 '18 at 00:46
  • @AndrewSchultz It was Woocommerce 2.5 in this time, So it has changed since Woocommerce 3… I Will make an update. – LoicTheAztec Oct 17 '18 at 01:23
  • Version 2.6.13 of WooCommerce returns an array of strings so they must have changed it somewhere between version 2.5 and 2.613. – Andrew Schultz Oct 17 '18 at 03:51