【问题标题】:Coupon with 2 differents fixed product discounts based on product IDs in WooCommerce基于 WooCommerce 中产品 ID 的 2 种不同固定产品折扣的优惠券
【发布时间】:2021-07-09 08:30:01
【问题描述】:

我遇到过

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

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

【问题讨论】:

    标签: php wordpress woocommerce product coupon


    【解决方案1】:

    您的代码包含几个错误

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

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

    第一步,优惠券设置:

    • 标题:summer21
    • 常规 > 折扣类型:固定产品折扣
    • General > 优惠券金额:20
    • 使用限制 > Products: where 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 );
    

    【讨论】:

    • 你的明星!谢谢,这很有魅力。不仅在购物车上而且在产品页面/商店等上显示的功能是什么?目前商店显示 75 英镑(20 英镑折扣),而购物车现在显示正确的 10 英镑折扣 85 英镑。
    • 刚刚注意到,这也将 95 英镑的产品显示为边车/迷你车中的 55 英镑,而购物篮/结帐时正确显示为 85 英镑
    • 如果您有新问题,请点击 按钮提出问题。如果有助于提供上下文,请包含指向此问题的链接。
    • 我会说这是原始问题的一部分
    • @HenryAspden 我的答案是基于您在问题中引用的答案代码,正如您在该答案中看到的那样,没有为其他页面的调整提供额外的代码,所以提出一个新问题或调整必要时提出您的问题
    猜你喜欢
    • 2021-06-10
    • 2018-12-12
    • 2021-12-31
    • 1970-01-01
    • 2020-08-30
    • 2018-03-24
    • 2013-12-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多