-1

I simply need to add a 10% discount to every product in the cart starting from the second one.

I tried many discount plugins but none of them suits my customer's needs.

I.e. I need this scenario:

Variable product 1 - € 100 Variable product 2 - € 200 Variable product 3 - € 300

Case 1

User buys product 2 He pays € 100

Case 2

User buys product 3 He pays € 300

Case 3 (with discount)

User buys product 3 and product 1 He pays € 390 (10% discount calculated on lowest price)

Case 4 (with discount)

User buys product 3 and product 2 He pays € 480 (10% discount calculated on lowest price)

Case 5 (with discount) User buys products 3,2,1 He pays € 570 (10% discount calculated on product 2 and 1)

Is it possible to create such a system?

Thank you in advance.

Yuri Refolo
  • 245
  • 9
  • 17

1 Answers1

3

Add this to your theme's 'functions.php'.

function add_discount_price_percent( $cart_object ) {
    global $woocommerce;
    $custom_discount_per = 10; // custom discount percent
    $pdtcnt=0;

    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
        $pdtcnt++;
        if($pdtcnt>1) { // from second product
            $oldprice = $cart_item['data']->price; //original product price
            $newprice = $oldprice - ($oldprice*($custom_discount_per/100)); //discounted price
            $cart_item['data']->set_price($newprice);
        }        
    }
}

add_action( 'woocommerce_before_calculate_totals', 'add_discount_price_percent' );
Outsource WordPress
  • 3,749
  • 2
  • 10
  • 23
  • Thank you. I didn't ask for a complete solution or a code but it's obviously appreciated and it does works perfectly, moreover. I tried numerous discounts plugin, bought two and tried a solution by myself but I'm not skilled with WC APIs so I didn't managed how to do it. – Yuri Refolo Aug 13 '18 at 17:29