Skip to content

Commit 6900c38

Browse files
author
Prabhu Ram
committed
MC-20648: Implement the changes
- added plugins for loading quote object with discounts - added additional metadata to be stored in db
1 parent 59ff709 commit 6900c38

File tree

8 files changed

+166
-7
lines changed

8 files changed

+166
-7
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ private function getDiscountValues($cartItem, $currencyCode)
9494
$discount = [];
9595
$amount = [];
9696
/* @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */
97-
$discountAmount = $value['discount'];
97+
$discountData = $value['discount'];
98+
$discountAmount = $discountData->getAmount();
9899
/* @var \Magento\SalesRule\Model\Rule $rule */
99100
$rule = $value['rule'];
100101
$discount['label'] = $rule ?: __('Discount');

app/code/Magento/QuoteGraphQl/Model/Resolver/Discounts.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ private function getDiscountValues(Quote $quote)
4949
/* @var \Magento\SalesRule\Model\Rule $rule*/
5050
$rule = $value['rule'];
5151
$discount['label'] = $rule ?: __('Discount');
52-
$amount['value'] = $value['discount'];
52+
/* @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */
53+
$discountData = $value['discount'];
54+
$amount['value'] = $discountData->getAmount();
5355
$amount['currency'] = $quote->getQuoteCurrencyCode();
5456
$discount['amount'] = $amount;
5557
$discountValues[] = $discount;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\SalesRule\Model\Plugin;
7+
8+
use Magento\Framework\Serialize\Serializer\Json;
9+
use Magento\SalesRule\Model\Rule\Action\Discount\DataFactory;
10+
use Magento\Framework\App\ObjectManager;
11+
12+
/**
13+
* Plugin for persisting discounts along with Quote Address
14+
*/
15+
class Discount
16+
{
17+
/**
18+
* @var Json
19+
*/
20+
private $json;
21+
22+
/**
23+
* @var \Magento\SalesRule\Model\Rule\Action\Discount\DataFactory
24+
*/
25+
protected $discountFactory;
26+
27+
/**
28+
* @param Json $json
29+
* @param DataFactory|null $discountDataFactory
30+
*/
31+
public function __construct(Json $json, DataFactory $discountDataFactory = null)
32+
{
33+
$this->json = $json;
34+
$this->discountFactory = $discountDataFactory ?: ObjectManager::getInstance()->get(DataFactory::class);
35+
}
36+
37+
/**
38+
* Plugin for adding item discounts to extension attributes
39+
*
40+
* @param \Magento\Quote\Model\Quote $subject
41+
* @param \Magento\Quote\Model\ResourceModel\Quote\Item\Collection $result
42+
* @return \Magento\Quote\Model\ResourceModel\Quote\Item\Collection
43+
*/
44+
public function afterGetItemsCollection(
45+
\Magento\Quote\Model\Quote $subject,
46+
\Magento\Quote\Model\ResourceModel\Quote\Item\Collection $result
47+
) {
48+
foreach ($result as $item) {
49+
if ($item->getDiscounts() && !$item->getExtensionAttributes()->getDiscounts()) {
50+
$discounts = $this->json->unserialize($item->getDiscounts());
51+
foreach ($discounts as $key => $value) {
52+
$discounts[$key]['discount'] = $this->unserializeDiscountData($value['discount']);
53+
}
54+
$itemExtension = $item->getExtensionAttributes();
55+
$itemExtension->setDiscounts($discounts);
56+
}
57+
}
58+
return $result;
59+
}
60+
61+
/**
62+
* Plugin for adding address level discounts to extension attributes
63+
*
64+
* @param \Magento\Quote\Model\Quote $subject
65+
* @param array $result
66+
* @return array
67+
*/
68+
public function afterGetAllAddresses(
69+
\Magento\Quote\Model\Quote $subject,
70+
array $result
71+
) {
72+
foreach ($result as $address) {
73+
if ($address->getDiscounts() && !$address->getExtensionAttributes()->getDiscounts()) {
74+
$discounts = $this->json->unserialize($address->getDiscounts());
75+
foreach ($discounts as $key => $value) {
76+
$discounts[$key]['discount'] = $this->unserializeDiscountData($value['discount']);
77+
}
78+
$itemExtension = $address->getExtensionAttributes();
79+
$itemExtension->setDiscounts($discounts);
80+
}
81+
}
82+
return $result;
83+
}
84+
85+
/**
86+
* Unserialize discount object
87+
*
88+
* @param string $serializedDiscount
89+
* @return \Magento\SalesRule\Model\Rule\Action\Discount\Data
90+
*/
91+
private function unserializeDiscountData(string $serializedDiscount)
92+
{
93+
$discountArray = $this->json->unserialize($serializedDiscount);
94+
$discountData = $this->discountFactory->create();
95+
$discountData->setBaseOriginalAmount($discountArray['baseOriginalAmount']);
96+
$discountData->setOriginalAmount($discountArray['originalAmount']);
97+
$discountData->setAmount($discountArray['amount']);
98+
$discountData->setBaseAmount($discountArray['baseAmount']);
99+
return $discountData;
100+
}
101+
}

app/code/Magento/SalesRule/Model/Plugin/ResourceModel/Discount.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public function beforeSave(
3939
foreach ($object->getAllAddresses() as $address) {
4040
$discounts = $address->getExtensionAttributes()->getDiscounts();
4141
if ($discounts) {
42+
foreach ($discounts as $key => $value) {
43+
$discount = $value['discount'];
44+
$discountData = [
45+
"amount" => $discount->getAmount(),
46+
"baseAmount" => $discount->getBaseAmount(),
47+
"originalAmount" => $discount->getOriginalAmount(),
48+
"baseOriginalAmount" => $discount->getBaseOriginalAmount()
49+
];
50+
$discounts[$key]['discount'] = $this->json->serialize($discountData);
51+
}
4252
$address->setDiscounts($this->json->serialize($discounts));
4353
}
4454
}

app/code/Magento/SalesRule/Model/Quote/Discount.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\SalesRule\Model\Quote;
77

8+
use Magento\SalesRule\Model\Rule\Action\Discount\DataFactory;
9+
use Magento\Framework\App\ObjectManager;
10+
811
/**
912
* Discount totals calculation model.
1013
*/
@@ -37,22 +40,31 @@ class Discount extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
3740
protected $priceCurrency;
3841

3942
/**
43+
* @var \Magento\SalesRule\Model\Rule\Action\Discount\DataFactory
44+
*/
45+
private $discountFactory;
46+
47+
/**
48+
* Discount constructor.
4049
* @param \Magento\Framework\Event\ManagerInterface $eventManager
4150
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
4251
* @param \Magento\SalesRule\Model\Validator $validator
4352
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
53+
* @param DataFactory|null $discountDataFactory
4454
*/
4555
public function __construct(
4656
\Magento\Framework\Event\ManagerInterface $eventManager,
4757
\Magento\Store\Model\StoreManagerInterface $storeManager,
4858
\Magento\SalesRule\Model\Validator $validator,
49-
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
59+
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
60+
DataFactory $discountDataFactory = null
5061
) {
5162
$this->setCode(self::COLLECTOR_TYPE_CODE);
5263
$this->eventManager = $eventManager;
5364
$this->calculator = $validator;
5465
$this->storeManager = $storeManager;
5566
$this->priceCurrency = $priceCurrency;
67+
$this->discountFactory = $discountDataFactory ?: ObjectManager::getInstance()->get(DataFactory::class);
5668
}
5769

5870
/**
@@ -91,12 +103,14 @@ public function collect(
91103

92104
$address->setDiscountDescription([]);
93105
$items = $this->calculator->sortItemsByPriority($items, $address);
106+
$address->getExtensionAttributes()->setDiscounts([]);
94107

95108
/** @var \Magento\Quote\Model\Quote\Item $item */
96109
foreach ($items as $item) {
97110
if ($item->getNoDiscount() || !$this->calculator->canApplyDiscount($item)) {
98111
$item->setDiscountAmount(0);
99112
$item->setBaseDiscountAmount(0);
113+
$item->getExtensionAttributes()->setDiscounts([]);
100114

101115
// ensure my children are zeroed out
102116
if ($item->getHasChildren() && $item->isChildrenCalculated()) {
@@ -233,14 +247,26 @@ private function aggregateDiscountPerRule(
233247
$discountBreakdown = $item->getExtensionAttributes()->getDiscounts();
234248
$discountPerRule = $address->getExtensionAttributes()->getDiscounts();
235249
if ($discountBreakdown) {
250+
/** @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */
236251
foreach ($discountBreakdown as $key => $value) {
237252
/* @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discount */
238253
$discount = $value['discount'];
239254
$ruleLabel = $value['rule'];
240255
if (isset($discountPerRule[$key])) {
241-
$discountPerRule[$key]['discount'] += $discount;
256+
$discountData = $discountPerRule[$key]['discount'];
257+
$discountData->setBaseAmount($discountData->getBaseAmount()+$discount->getBaseAmount());
258+
$discountData->setAmount($discountData->getAmount()+$discount->getAmount());
259+
$discountData->setOriginalAmount($discountData->getOriginalAmount()+$discount->getOriginalAmount());
260+
$discountData->setBaseOriginalAmount(
261+
$discountData->getBaseOriginalAmount()+$discount->getBaseOriginalAmount()
262+
);
242263
} else {
243-
$discountPerRule[$key]['discount'] = $discount;
264+
$discountData = $this->discountFactory->create();
265+
$discountData->setBaseAmount($discount->getBaseAmount());
266+
$discountData->setAmount($discount->getAmount());
267+
$discountData->setOriginalAmount($discount->getOriginalAmount());
268+
$discountData->setBaseOriginalAmount($discount->getBaseOriginalAmount());
269+
$discountPerRule[$key]['discount'] = $discountData;
244270
}
245271
$discountPerRule[$key]['rule'] = $ruleLabel;
246272
}

app/code/Magento/SalesRule/Model/Quote/Item/Plugin/Discount.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,20 @@ public function __construct(Json $json)
3737
public function beforeSave(CartItemPersister $subject, CartInterface $quote, CartItemInterface $cartItem)
3838
{
3939
$cartExtension = $cartItem->getExtensionAttributes();
40-
$cartItem->setDiscounts($this->json->serialize($cartExtension->getDiscounts()));
40+
$discounts = $cartExtension->getDiscounts();
41+
if ($discounts) {
42+
foreach ($discounts as $key => $value) {
43+
$discount = $value['discount'];
44+
$discountData = [
45+
"amount" => $discount->getAmount(),
46+
"baseAmount" => $discount->getBaseAmount(),
47+
"originalAmount" => $discount->getOriginalAmount(),
48+
"baseOriginalAmount" => $discount->getBaseOriginalAmount(),
49+
];
50+
$discounts[$key]['discount'] = $this->json->serialize($discountData);
51+
}
52+
$cartItem->setDiscounts($this->json->serialize($discounts));
53+
}
4154
return [$quote, $cartItem];
4255
}
4356
}

app/code/Magento/SalesRule/Model/RulesApplier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private function setDiscountBreakdown($discountData, $item, $rule, $address)
211211
$discount->setBaseAmount($discountData->getBaseAmount());
212212
$discount->setOriginalAmount($discountData->getOriginalAmount());
213213
$discountBreakdown = $item->getExtensionAttributes()->getDiscounts() ?? [];
214-
$discountBreakdown[$rule->getId()]['discount'] = $discountData->getAmount();
214+
$discountBreakdown[$rule->getId()]['discount'] = $discount;
215215
$discountBreakdown[$rule->getId()]['rule'] = $rule->getStoreLabel($address->getQuote()->getStore()) ?: __('Discount');
216216
$item->getExtensionAttributes()->setDiscounts($discountBreakdown);
217217
}

app/code/Magento/SalesRule/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
<type name="Magento\Quote\Model\Quote\Config">
4747
<plugin name="append_sales_rule_keys_to_quote" type="Magento\SalesRule\Model\Plugin\QuoteConfigProductAttributes"/>
4848
</type>
49+
<type name="Magento\Quote\Model\Quote">
50+
<plugin name="append_discounts_to_items" type="Magento\SalesRule\Model\Plugin\Discount"/>
51+
</type>
52+
<type name="Magento\Quote\Model\Quote\Config">
53+
<plugin name="append_sales_rule_keys_to_quote" type="Magento\SalesRule\Model\Plugin\QuoteConfigProductAttributes"/>
54+
</type>
4955
<type name="Magento\Framework\Module\Setup\Migration">
5056
<arguments>
5157
<argument name="compositeModules" xsi:type="array">

0 commit comments

Comments
 (0)