cancel
Showing results for 
Search instead for 
Did you mean: 

Price Cart Rule discount cheapest product

Price Cart Rule discount cheapest product

Hello,

can you help me about creating a Cart Price Rule in magento that can calculate the cheapest value of three products for discount. So, if the customer buy three different products from the same category, the discount amount should be the cheapest value from these three products.

I tried to edit (Buy X get Y free) Action, but it didn't work.

For example:
1 x T-shirt = 20$
1 x Hat = 10$
1 x Glasses = 30$
Subtotal = 60$
Discount = 10$(the cheapest value)
Total Amount = 50$

Many thanks,
Arbnor

4 REPLIES 4

Re: Price Cart Rule discount cheapest product

Hello @Arbnor1

 

You can add the discount in a shopping cart as shown in the following answer.

 

https://magento.stackexchange.com/a/104487/48571

 

Now you can modify the discount amount as you want.

 

In your case, you want the cheapest price from the cart items as a discount amount.

For this, you have to modify the public function collect given in the above link.

 

1) Add a constructor

 

protected $checkouSession;

public function __construct(
   \Magento\Checkout\Model\SessionFactory $checkouSession
){
   $this->checkouSession = $checkouSession;
}

2) Change the discount amount in collect function of Model Class

 

$checkouSession = $this->checkouSession->create();
$cartItems = $checkouSession->getQuote()->getAllVisibleItems();
$prices = [];
foreach ($cartItems as $item) {
    $prices[] = $item->getPrice();
}

$discountAmount = min($prices);

 

 

If you find my answer useful, Please click Kudos & Accept as Solution.

Re: Price Cart Rule discount cheapest product

Hi @Arbnor1

 

Basically, through Magento default promotional feature - shopping cart price rule - to offer such discount is not possible.

 

To enable and offer such elevate discount promotion, you should require to utilize ready-made third party extension available; else you would require to do custom development in Magento.

 

This Amasty provides Special Promotions , by utilizing it you can fulfill your promotion needs and create promotions like - 

Buy 3 items, get the cheapest (most expensive) free,

Buy 5 products, get the cheapest (most expensive) with a discount,

and more

 

Please let me know if still you have any query.

 

If issue resolved, then kindly Click Kudos & Accept as Solution.

Re: Price Cart Rule discount cheapest product

Hi @Mayur Bhuva

Thank you for your help.
Could you please take a look at this code:

 

public function calculate($rule, $item, $qty)
{
/** @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */
$discountData = $this->discountFactory->create();

$itemPrice = $this->validator->getItemPrice($item);
$baseItemPrice = $this->validator->getItemBasePrice($item);
$itemOriginalPrice = $this->validator->getItemOriginalPrice($item);
$baseItemOriginalPrice = $this->validator->getItemBaseOriginalPrice($item);

$x = $rule->getDiscountStep();
$y = $rule->getDiscountAmount();
if (!$x || $y > $x) {
return $discountData;
}
$buyAndDiscountQty = $x + $y;

$fullRuleQtyPeriod = floor($qty / $buyAndDiscountQty);
$freeQty = $qty - $fullRuleQtyPeriod * $buyAndDiscountQty;

$discountQty = $fullRuleQtyPeriod * $y;
if ($freeQty > $x) {
$discountQty += $freeQty - $x;
}

$discountData->setAmount($discountQty * $itemPrice);
$discountData->setBaseAmount($discountQty * $baseItemPrice);
$discountData->setOriginalAmount($discountQty * $itemOriginalPrice);
$discountData->setBaseOriginalAmount($discountQty * $baseItemOriginalPrice);

return $discountData;

}

 

A function for Buy X get Y free and it is working perfect in case when Buy three same products and discount one of them.


I think I should make the changes in this file.(path: vendor/magento/module-sales-rule/Model/Rule/Action/Discount/MyClass,php)
If you can help me how to do that.

Thank you

Re: Price Cart Rule discount cheapest product

Hello @Arbnor1

 

Use my code which I posted in my previous reply.

 

If you want to make changes in your MyClass.php file, you can set it as given below.

 

1) Get the discount amount

 

$checkouSession = $this->checkouSession->create();
$cartItems = $checkouSession->getQuote()->getAllVisibleItems();
$prices = [];
foreach ($cartItems as $item) {
    $prices[] = $item->getPrice();
}

$discountAmount = min($prices);

2) Set Discount Amount in your code

 

...
...
$discountData->setAmount($discountAmount);
$discountData->setBaseAmount($discountAmount);
$discountData->setOriginalAmount($discountAmount);
$discountData->setBaseOriginalAmount($discountAmount);
...
...

 

 

 

If you find my answer useful, Please click Kudos & Accept as Solution.