WooCommerce购物车仅限第二件商品享受常规折扣

时间:2020-05-04 07:28:33

标签: php wordpress woocommerce cart discount

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

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

您最好使用以下选项,该选项将仅计算常规商品(不包括“正在销售”的商品),并将根据从第二项开始的特定计数应用折扣:

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

答案 1 :(得分:1)

您是如此亲密。 您知道购物车$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;
}