Gaurav
Gaurav

Reputation: 201

Get coupon data from WooCommerce orders

I have created in WooCommerce two custom coupon types:

function custom_discount_type( $discount_types ) {
    $discount_types['cash_back_fixed'] =__( 'Cash Back fixed discount', 'woocommerce' );
     $discount_types['cash_back_percentage'] =__( 'Cash Back Percentage discount', 'woocommerce' );
         return $discount_types;

     }

add_filter( 'woocommerce_coupon_discount_types', 'custom_discount_type',10, 1);

I would like to get the discount type after Order status is "completed", something like :

function wc_m_move_order_money_to_user( $order_id, $old_status, $new_status ){

    if( $order->get_used_coupons() ) {
        if ($coupon->type == 'cash_back_fixed'){ 
           $coupons_amont =  ???
           ....

       }
    }
}

But $coupon->type doesn't work.

How can I get the coupon types used in the order?
And how can I get the original coupon amount?.

Thanks

Upvotes: 20

Views: 55354

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Update 3

Since WooCommerce 3.7, you should now use the WC_Abstract method get_coupon_codes() on the WC_Order instance object to get the used coupons from an order, as get_used_coupons() method is deprecated.

So you will replace in the code:

foreach( $order->get_used_coupons() as $coupon_code ){

by:

foreach( $order->get_coupon_codes() as $coupon_code ){

Then you can get coupon details like:

foreach( $order->get_coupon_codes() as $coupon_code ) {
    // Get the WC_Coupon object
    $coupon = new WC_Coupon($coupon_code);

    $discount_type = $coupon->get_discount_type(); // Get coupon discount type
    $coupon_amount = $coupon->get_amount(); // Get coupon amount
}

Addition: You can also get some coupon data from order items "coupon" like:

$order = wc_get_order( $order_id );

// Loop through coupon items for this order
foreach( $order->get_items('coupon') as $item ){
    $coupon_name          = $item->get_name(); // Coupon name
    $coupon_code          = $item->get_code(); // Coupon code
    $coupon_discount      = $item->get_discount(); // Discount amount
    $coupon_discount_tax  = $item->get_discount_tax(); // Discount tax amount

    $coupon               = new WC_Coupon($coupon_code); // Get the WC_Coupon object
    $coupon_discount_type = $coupon->get_discount_type(); // Coupon discount type
}

So here we use WC_Order_Item_Coupon methods.


Update 2

First you can't access anymore WC objects properties since WooCommerce 3.

You should now use WC_Coupon getter methods to get coupon details from the WC_Coupon Object instance…

In your case you have to use get_discount_type() method or is_type( 'cash_back_fixed' ) method …

Here is the way to do it:

// Get an instance of WC_Order object
$order = wc_get_order( $order_id );

// Coupons used in the order LOOP (as they can be multiple)
foreach( $order->get_used_coupons() as $coupon_code ){

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

    // Get an instance of WC_Coupon object in an array(necessary to use WC_Coupon methods)
    $coupon = new WC_Coupon($coupon_id);

    // Now you can get type in your condition
    if ( $coupon->get_discount_type() == 'cash_back_percentage' ){
        // Get the coupon object amount
        $coupon_amount1 = $coupon->get_amount();
    }

    // Or use this other conditional method for coupon type
    if( $coupon->is_type( 'cash_back_fixed' ) ){
        // Get the coupon object amount
        $coupon_amount2 = $coupon->get_amount();
    }
}

To get the coupons discount amounts (and to use also coupons types methods) here is the way:

$order = wc_get_order( $order_id );

// GET THE ORDER COUPON ITEMS
$order_items = $order->get_items('coupon');

// print_r($order_items); // For testing

// LOOP THROUGH ORDER COUPON ITEMS
foreach( $order_items as $item_id => $item ){

    // Retrieving the coupon ID reference
    $coupon_post_obj = get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' );
    $coupon_id = $coupon_post_obj->ID;

    // Get an instance of WC_Coupon object (necessary to use WC_Coupon methods)
    $coupon = new WC_Coupon($coupon_id);

    ## Filtering with your coupon custom types
    if( $coupon->is_type( 'cash_back_fixed' ) || $coupon->is_type( 'cash_back_percentage' ) ){

        // Get the Coupon discount amounts in the order
        $order_discount_amount = wc_get_order_item_meta( $item_id, 'discount_amount', true );
        $order_discount_tax_amount = wc_get_order_item_meta( $item_id, 'discount_amount_tax', true );

        ## Or get the coupon amount object
        $coupons_amount = $coupons->get_amount();
    }
}

So to get the coupon price, we use the WC_Coupon get_amount() method

Upvotes: 45

Meloman
Meloman

Reputation: 3712

In my case, I needed to change new orders to "pre-order" (custom status already added in system) if specific coupon used and order paid (so, if processing status engaged).

function change_status_to_preorder($order_id) {
    
    $order          = new WC_Order($order_id);
    $coupon_codes   = $order->get_coupon_codes();
    
    if(in_array('MYCOUPON', $coupon_codes) && $order->get_status() == 'processing') {
        $order->update_status('wc-pre-order', 'AUTO: note to shop manager');
    }
}
add_action('woocommerce_new_order', 'change_status_to_preorder', 1, 1);

Upvotes: 0

Related Questions