【问题标题】:WooCommerce cart discount from second item on regular items onlyWooCommerce 购物车折扣仅适用于常规商品的第二件商品
【发布时间】:2020-08-18 13:54:07
【问题描述】:

我在 stackoverflow 上找到了这段代码,它可以满足我的需要,除了我需要在购物车中添加 2 个产品后立即为每个产品应用 -5 欧元的折扣。

例子:

  • 对于购物车中的 1 件产品,用户将获得 0 欧元的折扣
  • 对于购物车中的 2 件产品,用户将获得 5 欧元的折扣
  • 对于购物车中的 3 件产品,用户将获得 10 欧元的折扣
  • 对于购物车中的 4 件产品,用户将获得 15 欧元的折扣
  • 对于购物车中的 5 件产品,用户将获得 20 欧元的折扣

等等……

add_action('woocommerce_cart_calculate_fees' , 'custom_discount', 10, 1);
function custom_discount( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) && ! is_user_logged_in() )
    // if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    // Only when there is 2 or more items in cart
    if( $cart->get_cart_contents_count() >= 2):

        // Initialising variable
        $is_on_sale = false;

        // Iterating through each item in cart
        foreach( $cart->get_cart() as $cart_item ){
            // Getting an instance of the product object
            $product =  $cart_item['data'];

            // If a cart item is on sale, $is_on_sale is true and we stop the loop
            if($product->is_on_sale()){
                $is_on_sale = true;
                break;
            }
        }

        ## Discount calculation ##
        // fixed reduction price
        $reduction = 5;


        ## Applied discount (no products on sale) ##
        if(!$is_on_sale )
            $cart->add_fee( '-5€ à partir du 2ème article commandé', -$reduction);

    endif;
}

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce cart discount


    【解决方案1】:

    您最好使用以下方法,仅计算常规商品(不是“特价”商品),并根据从第二件商品开始的特定计数应用折扣:

    add_action('woocommerce_cart_calculate_fees' , 'progressive_fixed_discount', 10, 1);
    function progressive_fixed_discount( $cart ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) && ! is_user_logged_in() )
        // if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // Initialising variable
        $regular_items_count = -1;
    
        // Iterating through each item in cart
        foreach( $cart->get_cart() as $cart_item ){
    
            // Count only on regular items (not "on sale" items)
            if( ! $cart_item['data']->is_on_sale() ){
                $regular_items_count += $cart_item['quantity'];
            }
        }
        
        // Only for regular items starting on the 2nd item (not "on sale" items)
        if ( $regular_items_count > 0 ) {
    
            // Progressive fixed discount calculation on regular items only
            $discount = 5 * $regular_items_count;
    
    
            // Apply a discount for "on sale" items only 
            $cart->add_fee( __("-5€ à partir du 2ème article commandé", "woocommerce"), -$discount );
        }
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    一些相关的similar answers threads


    添加 - 排除产品类别:

    替换:

    if( ! $cart_item['data']->is_on_sale() ){
    

    与:

    if( ! $cart_item['data']->is_on_sale() && ! has_term( array( 5537 ), 'product_cat', $cart_item['product_id'] ) ) {
    

    【讨论】:

    • 这很有魅力。我首先测试了第一个答案,除了打折商品外,它似乎有效
    • 排除一个产品类别:见这个答案的最后,我已经为你做了一个补充(uptaded)...
    【解决方案2】:

    你离得很近。 您知道购物车 $cart->get_cart_contents_count() 中的商品数量,并且您希望在第一个产品之后开始享受折扣。

    替换$reduction = 5;

    用这个$reduction = 5*($cart->get_cart_contents_count() - 1);

    您的完整代码应如下所示:

    add_action('woocommerce_cart_calculate_fees' , 'custom_discount', 10, 1);
    function custom_discount( $cart ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) && ! is_user_logged_in() )
        // if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
        // Only when there is 2 or more items in cart
        if( $cart->get_cart_contents_count() >= 2):
    
            // Initialising variable
            $is_on_sale = false;
    
            // Iterating through each item in cart
            foreach( $cart->get_cart() as $cart_item ){
                // Getting an instance of the product object
                $product =  $cart_item['data'];
    
                // If a cart item is on sale, $is_on_sale is true and we stop the loop
                if($product->is_on_sale()){
                    $is_on_sale = true;
                    break;
                }
            }
    
            ## Discount calculation ##
            // fixed reduction price
            $reduction = 5*($cart->get_cart_contents_count() - 1);
    
    
            ## Applied discount (no products on sale) ##
            if(!$is_on_sale )
                $cart->add_fee( '-5€ à partir du 2ème article commandé', -$reduction);
    
        endif;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 2019-01-31
      • 2019-02-06
      • 2020-11-24
      • 2019-06-28
      • 1970-01-01
      • 2019-06-13
      相关资源
      最近更新 更多