根据产品标签woocommerce应用不同的优惠券折扣百分比

问题描述 投票:1回答:1

[我已经创建了一个代码,用于仅在购物车上的优惠券“转售商”处于活动状态时,才根据产品是否包含标签“ big”在购物车上应用不同的折扣。

默认代码折扣:50%

具有标签“ big” 20%时的代码折扣

我忘记了一些东西,我没有让它起作用。

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


    // Related coupons codes to be defined in this array 

    $coupon_codes = array('resellers'); //coupon code

    // Product tags at 20% 


    $tag = array( "big" );   // for 20% discount
    $second_percentage = 0.2; // 20 %




    ## ----  Changing the percentage to 20% for specific a product tag ---- ##

    if ( $coupon->is_type('percent') && in_array( $coupon->get_code(), $coupon_codes ) ) {
        if( has_term( $tag, 'product_tag', $cart_item['product_id'] ) ){

            $discount = $second_percentage * $discounting_amount;
            $round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
        }
    }
    return $round;
}
php wordpress woocommerce product coupon
1个回答
0
投票

我已经解决了问题

这是代码:

 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 ---- ##


    $coupon_codes = array('relesllers');

    $tag = array('big'); // for 20% discount

    $second_percentage = 0.2; // 20 %




    if ( $coupon->is_type('percent') && in_array( $coupon->get_code(), $coupon_codes ) ) {
        if( has_term( $tag, 'product_tag', $cart_item['product_id'] ) ){

            $discount = $second_percentage * $discounting_amount;
            $round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
        }
    }
    return $round;
}



    ## ----  Changing the percentage to 20% for specific a product tag ---- ##

    if ( $coupon->is_type('percent') && in_array( $coupon->get_code(), $coupon_codes ) ) {
        if( has_term( $tag, 'product_tag', $cart_item['product_id'] ) ){

            $discount = $second_percentage * $discounting_amount;
            $round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
        }
    }
    return $round;
}
© www.soinside.com 2019 - 2024. All rights reserved.