1

I want to achieve a global discount for all products but only for the 2nd product item.

What do I mean? If the customer buys "Jacket" no discount is given.

Customer buys two of the "Jacket" product. 20% discount is given on the second "Jacket".

Customer buys five of the "Jacket" product. Still only 20% discount on the 2nd "Jacket".

It should work for all both simple and variable products.

I managed to figure this much out and I'm asking for help with how to make the discount apply to only the 2nd item.

Here's the code:

add_filter('woocommerce_product_get_price', 'fifty_percent_on_second_product', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'fifty_percent_on_second_product', 90, 2 );
function fifty_percent_on_second_product( $price, $product ) {
    if ( is_user_logged_in() ) { 

        I do not know what to do here

}   
    return $price;   
}

Does anyone know how to make this work? If so, please help me.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

3 Answers3

3

You could use the Fee API to get a discount (20%) on the 2nd item only this way:

add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 20; // 20%
    $discount   = 0;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // When quantity is more than 1
        if( $cart_item['quantity'] > 1 ){
            // 20% of the product price as a discount
            $discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
        }
    }
    if( $discount > 0 )
        $cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for your answer, but it gives 20% discount on the total sum, not just the 2nd item. Scenario (like I explained in my question): "Jacket" costs 20 dollar. Customer buys one. No discount is given. Customer buys three. Four dollar discount is given (20% on 20 dollar = 20% off on the 2nd "Jacket"). Customer buys 5 "Jacket". Discount given is 4 dollar still. Does it makes sense? –  Jan 19 '19 at 13:21
  • also, the text "2nd item discount" shows no matter what. –  Jan 19 '19 at 13:27
  • my apologies, it works. only problem is; "2nd item discount" text is shown even though there is no discount yet. –  Jan 19 '19 at 13:29
  • if you have time. do you mind adding support for categories to the code? Would be nice to tie it down to one or more categories. I tried adding if has_term but it broke the whole thing.. –  Jan 19 '19 at 20:12
  • Here is the new question, thank you for having a look. https://stackoverflow.com/questions/54271364/quantity-discount-for-2nd-item-only-based-on-product-category –  Jan 19 '19 at 21:03
  • Working a charm, simple can be upgraded. – Rodrigo Zuluaga Nov 22 '20 at 15:58
0
add_action( 'woocommerce_before_calculate_totals', 'set_the_discount' );

function set_the_discount( $cart ) {

foreach ( $cart->cart_contents as $key => $value ) {    

    //please check whether your item is the second item of your cart or not?

    //if this is your second item in your cart

    //Set the 50% discount

    $product_id = $value['product_id'];
    $product = wc_get_product($product_id);
    $get_price = $product->get_price();
    $new_price = $get_price / 2;
    $value['data']->set_price($new_price);
  }

}
This hook can be used to set the price directly to the cart. Hope this may help
Risha Tiwari
  • 111
  • 4
  • that sets the price to 50% when buying two or more, which is not the plan. I can see from your comments that you write `// please check whether your item is the second item of your cart or not?` and that is the part I need to work. –  Jan 19 '19 at 11:58
  • Please check my another answer :), hope that will help you out! – Risha Tiwari Jan 19 '19 at 12:42
  • foreach ( $cart->cart_contents as $key => $values ) { $product_id = $values['product_id']; foreach ($cart->get_cart_item_quantities() as $key => $value){ //print_r($key[1]); $key = array_keys($cart->get_cart_item_quantities())[1]; if($key == $product_id){ $product = wc_get_product($product_id); $get_price = $product->get_price(); $new_price = $get_price / 2; $values['data']->set_price($new_price); break; } } } Use in same hook i.e "woocommerce_before_calculate_totals" – Risha Tiwari Jan 19 '19 at 12:43
0
foreach ( $cart->cart_contents as $key => $values ) {
                    $product_id = $values['product_id'];

                    foreach ($cart->get_cart_item_quantities() as $key => $value){
                        //print_r($key[1]);
                        $key = array_keys($cart->get_cart_item_quantities())[1];
                        if($key == $product_id){

                            $product = wc_get_product($product_id);
                            $get_price = $product->get_price();
                            $new_price = $get_price / 2;
                            $values['data']->set_price($new_price);

                            break;
                        }
                    }
                }
//Please try this, it will work for sure in same above hook :)
Risha Tiwari
  • 111
  • 4