首页 文章

通过优惠券为非折扣商品添加自定义捆绑购物车折扣

提问于
浏览
2

我有Woocommerce购物车,我成功添加了“捆绑”折扣,如果购物车数量为5或更多,可以在购物车中的每件商品上享受7美元折扣 .

我还希望为某些产品启用优惠券 . 但我不想把我的捆绑折扣和优惠券折扣叠加在一起 .

以下是我目前添加捆绑折扣的方式:

add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

$discount = 0;
$cart_item_count = $cart_object->get_cart_contents_count();

if( $cart_item_count > 4 )
    $percent = 25;


$discount -= $cart_object->get_cart_contents_count() * 7;

if( $percent > 0 )
    $cart_object->add_fee( __( "Bundle discount", "woocommerce" ), $discount, true);
}

For example:
我的购物车中有5件商品,其中1件商品已打折,优惠券可享受35%的优惠 .
有没有办法通过应用优惠券仅对4个非打折商品保持捆绑折扣?

我不想再叠加7美元加35%的折扣 . 我只希望优惠券具有优先权,如果它被应用而不会失去我的其他4个项目的折扣 .

1 回答

  • 2

    这里是钩子功能代码,当购物车中至少有5件商品时,会添加 for each non discounted cart items 折扣 $7

    add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
    function wc_cart_quantity_discount( $cart_object ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $cart_total_count = $cart_object->get_cart_contents_count();
        $cart_items_count = 0;
    
        // Counting non discounted cart items (by a coupon discount)
        foreach( $cart_object->get_cart() as $cart_item_key => $cart_item )
            if( $cart_item['line_subtotal'] == $cart_item['line_total'] )
                $cart_items_count += $cart_item['quantity']; 
    
        if( $cart_total_count > 4 ){
            $discount = $cart_items_count * 7;
            $cart_object->add_fee( __( "Bundle discount", "woocommerce" ), -$discount, true);
        }
    }
    

    代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .

    此代码经过测试并可以使用 .

相关问题