php – 在WooCommerce订单中获取优惠券折扣类型和金额

我在WooCommerce中创建了两种自定义优惠券类型:

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

订单状态为“已完成”后,我想获得折扣类型,例如:

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 =  ???
           ....

       }
    }
}

但$coupon->类型不起作用.

如何获得订单中使用的优惠券类型?
我怎样才能获得原始优惠券金额?

谢谢

解决方法:

更新

首先,您不能再访问WC对象属性,因为WooCommerce 3并且您需要使用WC_Coupon get_discount_type()方法is_type( 'cash_back_fixed' )方法

这是做到这一点的方法

// 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_name ){

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

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

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

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

要获得优惠券折扣金额(并使用优惠券类型方法),这里的方法是:

global $wpdb;
$order = wc_get_order( $order_id );

## GET THE ORDER LINE ITEMS
$table = $wpdb->prefix . "woocommerce_order_items";
$order_items = $wpdb->get_results( "SELECT  *  FROM  $table  WHERE  order_id = $order_id ");

print_r($order_items);

## GET THE COUPONS AMOUNTS IN THE ORDER
foreach( $order_items as $item_values ){

    // targeting "coupon" order item type
    if( 'coupon' == $item_values->order_item_type ){

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

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

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

            // Get the corresponding Item ID
            $item_id = $item_values->order_item_id;

            // 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_obj->get_amount();
        }
    }
}

Now to get the WC_Coupon price, just use the get_amount() method

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...