【问题标题】:Discount for a specific product category in WooCommerce cart based on quantity and multiples基于数量和倍数的 WooCommerce 购物车中特定产品类别的折扣
【发布时间】:2020-10-28 11:46:12
【问题描述】:

我目前的折扣基本上是说

  • 如果购物车中有特定类别中的 6 件产品 --- 总价折扣 10 美元

这段代码运行良好。我的问题是,这不起作用

  • 如果有 7 个产品,则第 7 个产品属于不同的类别。

我的目标是无论购物车中有多少产品,只要“A 类”中有 6 种产品,我都会给予折扣。

只要“折扣类别”中有 6 个产品或有 1 个数量为 6 的产品等,以下代码就可以工作。当我添加另一个类别的产品时,它会崩溃。随意撕开它。

add_action( 'woocommerce_before_calculate_totals', 'this_item_free' );

function this_item_free() {

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $valid_product_category_id = array('soup-mix');
    $has_category = false;

    $count = WC()->cart->get_cart_contents_count();

    foreach ( WC()->cart->get_cart() as $product ) {
        $quantity = $product['quantity'];
        $price = $product['data']->get_price();
    }

    if ( has_term( $valid_product_category_id, 'product_cat', $product['product_id'],
            $product['quantity'] ) ) {
        $has_category = true;
        $cart_total = floatval( preg_replace( '#[^\d.]#', '', WC()->cart->get_cart_total() ) );

        if($count == 6 && $has_category = true){
            add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total', 25, 1 );
            function discount_based_on_total( $cart ) {

                $total = $cart->cart_contents_total;
                $discount = 9.95;

                $cart->add_fee( __('discount', 'woocommerce'), -$discount );
                wc_add_notice( apply_filters( 'discount_applied', 'You just got a free soup!') );

            }
        }
    }
}

【问题讨论】:

    标签: wordpress woocommerce categories cart


    【解决方案1】:

    您的代码包含一些不必要的步骤,只需 woocommerce_cart_calculate_fees 操作挂钩即可获得您想要实现的目标

    我的回答包含:

    无论购物车中有多少产品,只要“A 类”中有 6 件产品或 1 件数量为 6 的产品等,都给予折扣。

    function action_woocommerce_cart_calculate_fees( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        /* SETTINGS */
    
        // Specific categories
        $specific_categories = array( 'Categorie-A' );
    
        // Discount
        $discount = 10;
        
        // Min quantity
        $minimun_quantity = 6;
    
        /* END SETTINGS */
        
        // Counter
        $current_quantity = 0;
    
        // Loop though each cart item
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get product id
            $product_id = $cart_item['product_id'];
    
            // Has certain category     
            if ( has_term( $specific_categories, 'product_cat', $product_id ) ) {
                // Quantity
                $product_quantity = $cart_item['quantity'];
    
                // Add to total
                $current_quantity += $product_quantity;
            }
        }
    
        // Greater than or equal to
        if ( $current_quantity >= $minimun_quantity ) {          
            // Add fee
            $cart->add_fee( __( 'Discount_applied', 'woocommerce' ), -$discount, false );      
        }
    }
    add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
    

    应用每 6 件产品的折扣(6 = 10 美元、12 = 20 美元、18 = 30 美元等)

    替换

    // Greater than or equal to
    if ( $current_quantity >= $minimun_quantity ) {          
        // Add fee
        $cart->add_fee( __( 'Discount_applied', 'woocommerce' ), -$discount, false );      
    }
    

    有

    // Greater than or equal to
    if ( $current_quantity >= $minimun_quantity ) {
        // Modulo
        $mod = $current_quantity % $minimun_quantity;
        
        // Times it fit
        $times = ( $current_quantity - $mod ) / $minimun_quantity;
    
        // Discount * times
        $discount = $discount * $times;
        
        // Add fee
        $cart->add_fee( __( 'Discount_applied', 'woocommerce' ), -$discount, false );      
    }
    

    【讨论】:

    • 感谢您的输入和快速响应,我的功能非常丑陋,我也想更改它。我真的很感谢你的帮助!效果很好。
    • 只是出于好奇.. 我怎么能以 6 的倍数做到这一点?换句话说,对于“A 类”中的每 6 件产品,是 10 美元? 6 = 10 美元 12 = 20 美元 18 = 30 美元等等。
    • @BrionBallard 查看更新的答案
    • 非常感谢,我阅读了模数但没有尝试使用它。一切都很完美,感谢您的帮助。
    猜你喜欢
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2021-05-18
    相关资源
    最近更新 更多