Get coupon data from WooCommerce orders

Viewed 43228

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

2 Answers

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);
Related