Skip to content

Commit

Permalink
Merge pull request #5425 from magento-mpi/MC-29276
Browse files Browse the repository at this point in the history
MC-29276: Discount fixed amount whole cart applied mutiple time when customer use Check Out with Multiple Addresses
  • Loading branch information
dhorytskyi authored Mar 5, 2020
2 parents 39914f7 + 488dfd0 commit 90012f2
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,13 @@ public function calculate($rule, $item, $qty)
$ruleTotals = $this->validator->getRuleItemTotalsInfo($rule->getId());

$quote = $item->getQuote();
$address = $item->getAddress();

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

/**
* prevent applying whole cart discount for every shipping order, but only for first order
*/
if ($quote->getIsMultiShipping()) {
$usedForAddressId = $this->getCartFixedRuleUsedForAddress($rule->getId());
if ($usedForAddressId && $usedForAddressId != $address->getId()) {
return $discountData;
} else {
$this->setCartFixedRuleUsedForAddress($rule->getId(), $address->getId());
}
}
$cartRules = $address->getCartFixedRules();
$cartRules = $quote->getCartFixedRules();
if (!isset($cartRules[$rule->getId()])) {
$cartRules[$rule->getId()] = $rule->getDiscountAmount();
}
Expand Down Expand Up @@ -122,14 +110,15 @@ public function calculate($rule, $item, $qty)
$discountData->setOriginalAmount(min($itemOriginalPrice * $qty, $quoteAmount));
$discountData->setBaseOriginalAmount($this->priceCurrency->round($baseItemOriginalPrice));
}
$address->setCartFixedRules($cartRules);
$quote->setCartFixedRules($cartRules);

return $discountData;
}

/**
* Set information about usage cart fixed rule by quote address
*
* @deprecated should be removed as it is not longer used
* @param int $ruleId
* @param int $itemId
* @return void
Expand All @@ -142,6 +131,7 @@ protected function setCartFixedRuleUsedForAddress($ruleId, $itemId)
/**
* Retrieve information about usage cart fixed rule by quote address
*
* @deprecated should be removed as it is not longer used
* @param int $ruleId
* @return int|null
*/
Expand Down
24 changes: 24 additions & 0 deletions app/code/Magento/SalesRule/Model/Rule/QuoteResetAppliedRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SalesRule\Model\Rule;

use Magento\SalesRule\Model\Spi\QuoteResetAppliedRulesInterface;

/**
* Reset applied rules to quote
*/
class QuoteResetAppliedRules implements QuoteResetAppliedRulesInterface
{
/**
* @inheritDoc
*/
public function execute(\Magento\Quote\Api\Data\CartInterface $quote): void
{
$quote->setCartFixedRules([]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SalesRule\Model\Spi;

/**
* Reset applied rules to quote
*/
interface QuoteResetAppliedRulesInterface
{
/**
* Reset applied rules to quote
*
* @param \Magento\Quote\Api\Data\CartInterface $quote
* @return void
*/
public function execute(\Magento\Quote\Api\Data\CartInterface $quote): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SalesRule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\SalesRule\Model\Spi\QuoteResetAppliedRulesInterface;

/**
* Reset applied rules to quote before collecting totals
*/
class QuoteResetAppliedRulesObserver implements ObserverInterface
{
/**
* @var QuoteResetAppliedRulesInterface
*/
private $resetAppliedRules;

/**
* @param QuoteResetAppliedRulesInterface $resetAppliedRules
*/
public function __construct(QuoteResetAppliedRulesInterface $resetAppliedRules)
{
$this->resetAppliedRules = $resetAppliedRules;
}

/**
* @inheritDoc
*/
public function execute(Observer $observer)
{
$this->resetAppliedRules->execute($observer->getQuote());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ protected function setUp()
$this->item = $this->createMock(\Magento\Quote\Model\Quote\Item\AbstractItem::class);
$this->data = $this->createPartialMock(\Magento\SalesRule\Model\Rule\Action\Discount\Data::class, []);

$this->quote = $this->createMock(\Magento\Quote\Model\Quote::class);
$this->quote = $this->createPartialMock(
\Magento\Quote\Model\Quote::class,
[
'getStore',
'getCartFixedRules',
'setCartFixedRules',
]
);
$this->address = $this->createPartialMock(
\Magento\Quote\Model\Quote\Address::class,
['getCartFixedRules', 'setCartFixedRules', '__wakeup']
['__wakeup']
);
$this->item->expects($this->any())->method('getQuote')->will($this->returnValue($this->quote));
$this->item->expects($this->any())->method('getAddress')->will($this->returnValue($this->address));
Expand Down Expand Up @@ -101,7 +108,7 @@ public function testCalculate()
{
$this->rule->setData(['id' => 1, 'discount_amount' => 10.0]);

$this->address->expects($this->any())->method('getCartFixedRules')->will($this->returnValue([]));
$this->quote->expects($this->any())->method('getCartFixedRules')->will($this->returnValue([]));
$store = $this->createMock(\Magento\Store\Model\Store::class);
$this->priceCurrency->expects($this->atLeastOnce())->method('convert')->will($this->returnArgument(0));
$this->priceCurrency->expects($this->atLeastOnce())->method('round')->will($this->returnArgument(0));
Expand Down Expand Up @@ -145,7 +152,7 @@ public function testCalculate()
$this->returnValue(100)
);

$this->address->expects($this->once())->method('setCartFixedRules')->with([1 => 0.0]);
$this->quote->expects($this->once())->method('setCartFixedRules')->with([1 => 0.0]);
$this->model->calculate($this->rule, $this->item, 1);

$this->assertEquals($this->data->getAmount(), 10);
Expand Down
2 changes: 2 additions & 0 deletions app/code/Magento/SalesRule/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
type="Magento\SalesRule\Model\Data\DiscountData" />
<preference for="Magento\SalesRule\Model\Spi\RuleQuoteRecollectTotalsInterface"
type="\Magento\SalesRule\Model\Rule\RuleQuoteRecollectTotalsOnDemand" />
<preference for="Magento\SalesRule\Model\Spi\QuoteResetAppliedRulesInterface"
type="\Magento\SalesRule\Model\Rule\QuoteResetAppliedRules" />
<type name="Magento\SalesRule\Helper\Coupon">
<arguments>
<argument name="couponParameters" xsi:type="array">
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/SalesRule/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@
<event name="salesrule_rule_delete_after">
<observer name="salesrule_quote_recollect_totals_on_delete" instance="\Magento\SalesRule\Observer\RuleQuoteRecollectTotalsObserver" />
</event>
<event name="sales_quote_collect_totals_before">
<observer name="salesrule_sales_quote_collect_totals_before" instance="\Magento\SalesRule\Observer\QuoteResetAppliedRulesObserver" />
</event>
</config>
Loading

0 comments on commit 90012f2

Please sign in to comment.