1

I found this piece of code on stackoverflow, it does what i need, except that i need to apply -5€ discount for each products right after 2 products are added in the cart.

Example:

  • for 1 products in the cart the user will get 0€ discount
  • for 2 products in the cart the user will get 5€ discount
  • for 3 products in the cart the user will get 10€ discount
  • for 4 products in the cart the user will get 15€ discount
  • for 5 products in the cart the user will get 20€ discount

and so on...

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

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
user832375
  • 89
  • 1
  • 1
  • 7

2 Answers2

2

You should better use the following that will count only regular items (not "on sale" items) and will apply a discount based on that specific count starting on the 2nd item:

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 );
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


Some related similar answers threads:


Addition - Excluding a product category:

Replace:

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

with:

if( ! $cart_item['data']->is_on_sale() && ! has_term( array( 5537 ), 'product_cat', $cart_item['product_id'] ) ) {
Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    this works as a charm. I had tested the first answer first and it seemed to work except for the discount items – user832375 May 04 '20 at 11:44
  • To exclude a product category: See at the end of this answer, I have made an addition for you ***(uptaded)***… – LoicTheAztec May 04 '20 at 15:23
1

You are so close. You know the number of items in cart $cart->get_cart_contents_count() and you want the discount to start after the first product.

Replace $reduction = 5;

with this $reduction = 5*($cart->get_cart_contents_count() - 1);

Your full code should look like this:

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;
}
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
kaize
  • 793
  • 1
  • 9
  • 17