从 WooCommerce 订单中获取优惠券数据 [英] Get coupon data from WooCommerce orders

查看:33
本文介绍了从 WooCommerce 订单中获取优惠券数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

function custom_discount_type( $discount_types ) {$discount_types['cash_back_fixed'] =__( '现金返还固定折扣', 'woocommerce');$discount_types['cash_back_percentage'] =__( '现金返还百分比折扣', 'woocommerce');返回 $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->type 不起作用.

如何获取订单中使用的优惠券类型?
以及如何获取原始优惠券金额?.

谢谢

解决方案

更新 3

从 WooCommerce 3.7 开始,您现在应该使用 WC_Abstract 方法 get_coupon_codes() 以从订单中获取使用过的优惠券,如get_used_coupons() 方法已弃用.

所以你将在代码中替换:

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

作者:

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

然后您可以获得优惠券详细信息,例如:

foreach( $order->get_coupon_codes() as $coupon_code ) {//获取 WC_Coupon 对象$coupon = new WC_Coupon($coupon_code);$discount_type = $coupon->get_discount_type();//获取优惠券折扣类型$coupon_amount = $coupon->get_amount();//获取优惠券金额}


更新 2

首先,从 WooCommerce 3 开始,您无法再访问 WC 对象属性.

<块引用>

您现在应该使用 WC_Coupon getter方法WC_Coupon对象实例中获取优惠券详情...

在您的情况下,您必须使用 get_discount_type() 方法或 is_type('cash_back_fixed') 方法……

方法如下:

//获取 WC_Order 对象的实例$order = wc_get_order( $order_id );//订单 LOOP 中使用的优惠券(因为它们可以是多个)foreach( $order->get_used_coupons() as $coupon_code ){//获取优惠券ID$coupon_post_obj = get_page_by_title($coupon_code, OBJECT, 'shop_coupon');$coupon_id = $coupon_post_obj->ID;//获取数组中WC_Coupon 对象的实例(需要使用WC_Coupon 方法)$coupon = new WC_Coupon($coupon_id);//现在你可以输入你的条件if ( $coupon->get_discount_type() == 'cash_back_percentage' ){//获取优惠券对象金额$coupon_amount1 = $coupon->get_amount();}//或者对优惠券类型使用其他条件方法if($coupon->is_type('cash_back_fixed')){//获取优惠券对象金额$coupon_amount2 = $coupon->get_amount();}}


要获得优惠券折扣金额(并使用优惠券类型方法),方法如下:

$order = wc_get_order( $order_id );//获取订单优惠券项目$order_items = $order->get_items('coupon');//print_r($order_items);//用于检测//通过订单优惠券项目循环foreach( $order_items as $item_id => $item ){//检索优惠券 ID 引用$coupon_post_obj = get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' );$coupon_id = $coupon_post_obj->ID;//获取WC_Coupon 对象的实例(需要使用WC_Coupon 方法)$coupon = new WC_Coupon($coupon_id);## 使用您的优惠券自定义类型进行过滤if($coupon->is_type('cash_back_fixed') || $coupon->is_type('cash_back_percentage')){//获取订单中的Coupon折扣金额$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 );## 或者获取优惠券金额对象$coupons_amount = $coupons->get_amount();}}


<块引用>

所以要获得优惠券价格,我们使用WC_Coupon get_amount()方法

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

解决方案

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
}


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

这篇关于从 WooCommerce 订单中获取优惠券数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆