基于 WooCommerce 中的产品 ID 的 2 种不同固定产品折扣的优惠券

问题描述

我遇到过

Coupon with 2 different percentage discounts based on product category in Woocommerce

其中显示了如何更改设置类别的折扣百分比。我想在我的商店中实施一个夏季折扣代码,为选择的产品提供 20 英镑的折扣。

优惠券代码 summer21 设置为仅对以下允许的产品 ID 应用固定产品折扣

  • 2523、119800、119886 和 120253

对于 4 个中的 3 个,应享受 20 英镑的折扣

但是如果产品 ID = 119886,那么应该只打折 10 英镑


我尝试将代码修改为以下内容,但不幸的是没有达到预期的结果。我错过了什么?

add_filter( 'woocommerce_coupon_get_discount_amount','alter_shop_coupon_data',20,5 );
function alter_shop_coupon_data( $round,$discounting_amount,$cart_item,$single,$coupon ){

    ## ---- Your settings ---- ##

    // Related coupons codes to be defined in this array (you can set many)
    $coupon_codes = array('Summer21');

    // Product categories for only £10 discount (IDs,Slugs or Names)
    $product_category10 = array('119886'); // for £10 discount

    $second_discount = 10; // £10

    ## ---- The code: Changing the discount to £10 for specific products ---- ##

    if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(),$coupon_codes ) ) {
        if( has_term( $product_category10,'product_cat',$cart_item['product_id'] ) ){
            $discount = $discounting_amount - $second_discount;
            $round = round( min( $discount,$discounting_amount ),wc_get_rounding_precision() );
        }
    }
    return $round;
}

此代码也仅更改购物车价格,但并未在产品页面上显示正确的显示价格。

解决方法

你的代码有几个错误

  • has_term() 不适用,我们使用 in_array() 代替
  • 不考虑每个产品的购物车商品数量。

为了使这个答案有效,我们应用了一些步骤

第一步,优惠券设置:

  • 标题:summer21
  • 一般 > 折扣类型:固定产品折扣
  • 一般 > 优惠券金额:20
  • 使用限制 > 产品:其中 productID:2523、119800、119886、120253

第二步,代码功能里面的设置:

  • 优惠券代码:将您的优惠券代码设置为小写
  • 产品 ID,第二次折扣:119886
  • 设置第二个折扣:10

所以你得到:

function filter_woocommerce_coupon_get_discount_amount( $round,$discounting_amount,$cart_item,$single,$coupon ) {   
    // Related coupons codes to be defined in this array
    $coupon_codes = array( 'summer21' );

    // Product IDs,for second discount
    $product_ids = array( 119886 ); 

    // Second discount
    $second_discount = 10;

    // Changing the discount for specific product Ids
    if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(),$coupon_codes ) ) {                
        // Search for product ID in array product IDs
        if ( in_array( $cart_item['product_id'],$product_ids ) ) {
            // Get cart item quantity
            $cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity'];
            
            // Discount
            $discount = $coupon->get_amount() - $second_discount;           
            $discount = $single ? $discount : $discount * $cart_item_qty;
            
            $round = round( min( $discount,$discounting_amount ),wc_get_rounding_precision() );
        }       
    }
    
    return $round;
}
add_filter( 'woocommerce_coupon_get_discount_amount','filter_woocommerce_coupon_get_discount_amount',10,5 );

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...