php - 从 WooCommerce 订单中获取优惠券数据

标签 php wordpress woocommerce orders coupon

我在 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->type 不起作用。

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

谢谢

最佳答案

更新 3

从 WooCommerce 3.7 开始,您现在应该使用 WC_Abstract 方法 get_coupon_codes()WC_Order 实例对象上从订单中获取已使用的优惠券,因为 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 ) {
    // 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
}

更新 2

首先,自 WooCommerce 3 以来,您无法再访问 WC 对象属性。

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

在您的情况下,您必须使用 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_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();
    }
}

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

$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

关于php - 从 WooCommerce 订单中获取优惠券数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44977174/

相关文章:

html - iframe 中的 PDF 文件与 IE 11 上的顶部导航菜单重叠

php - 使用 PHP 转换 WooCommerce Webhook 有效负载

php - Woocommerce 网站 6-7 第二页加载

wordpress - Woocommerce 2.6 API v1 -> 参数无效

php - 通过 PHP 上传文件,不起作用

php - 我为什么要购买这些 Silex 404?

wordpress - 如何获取 .js 和 .css 文件并替换它们

Wordpress Woocommerce 将所有产品更改为缺货

php - 使用 php 更新现有的 mysql 记录

php - 撇号和引号不会从表单添加到 mysql