Skip to content

Commit c29e323

Browse files
committed
#27338: Changes for SalesRule/Model/Quote/Discount , for fix if exist Extension Attributes
1 parent 517bc6e commit c29e323

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\App\ObjectManager;
99
use Magento\SalesRule\Api\Data\RuleDiscountInterfaceFactory;
1010
use Magento\SalesRule\Api\Data\DiscountDataInterfaceFactory;
11+
use Magento\Quote\Api\Data\AddressExtensionFactory;
1112

1213
/**
1314
* Discount totals calculation model.
@@ -50,21 +51,28 @@ class Discount extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
5051
*/
5152
private $discountDataInterfaceFactory;
5253

54+
/**
55+
* @var AddressExtensionFactory
56+
*/
57+
private $addressExtensionFactory;
58+
5359
/**
5460
* @param \Magento\Framework\Event\ManagerInterface $eventManager
5561
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
5662
* @param \Magento\SalesRule\Model\Validator $validator
5763
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
5864
* @param RuleDiscountInterfaceFactory|null $discountInterfaceFactory
5965
* @param DiscountDataInterfaceFactory|null $discountDataInterfaceFactory
66+
* @param AddressExtensionFactory|null $addressExtensionFactory
6067
*/
6168
public function __construct(
6269
\Magento\Framework\Event\ManagerInterface $eventManager,
6370
\Magento\Store\Model\StoreManagerInterface $storeManager,
6471
\Magento\SalesRule\Model\Validator $validator,
6572
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
6673
RuleDiscountInterfaceFactory $discountInterfaceFactory = null,
67-
DiscountDataInterfaceFactory $discountDataInterfaceFactory = null
74+
DiscountDataInterfaceFactory $discountDataInterfaceFactory = null,
75+
AddressExtensionFactory $addressExtensionFactory = null
6876
) {
6977
$this->setCode(self::COLLECTOR_TYPE_CODE);
7078
$this->eventManager = $eventManager;
@@ -75,6 +83,8 @@ public function __construct(
7583
?: ObjectManager::getInstance()->get(RuleDiscountInterfaceFactory::class);
7684
$this->discountDataInterfaceFactory = $discountDataInterfaceFactory
7785
?: ObjectManager::getInstance()->get(DiscountDataInterfaceFactory::class);
86+
$this->addressExtensionFactory = $addressExtensionFactory
87+
?: ObjectManager::getInstance()->get(AddressExtensionFactory::class);
7888
}
7989

8090
/**
@@ -84,6 +94,7 @@ public function __construct(
8494
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
8595
* @param \Magento\Quote\Model\Quote\Address\Total $total
8696
* @return $this
97+
* @throws \Magento\Framework\Exception\NoSuchEntityException
8798
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
8899
* @SuppressWarnings(PHPMD.NPathComplexity)
89100
*/
@@ -119,7 +130,20 @@ public function collect(
119130

120131
$address->setDiscountDescription([]);
121132
$items = $this->calculator->sortItemsByPriority($items, $address);
122-
$address->getExtensionAttributes()->setDiscounts([]);
133+
134+
if (!is_object($address->getExtensionAttributes())) {
135+
$addressExtensionAttributes = $address->getExtensionAttributes();
136+
$addressExtension = $this->addressExtensionFactory->create();
137+
138+
foreach ($addressExtensionAttributes as $key => $value) {
139+
$addressExtension->setData($key, $value);
140+
}
141+
$addressExtension->setDiscounts([]);
142+
$address->setExtensionAttributes($addressExtension);
143+
} else {
144+
$address->getExtensionAttributes()->setDiscounts([]);
145+
}
146+
123147
$addressDiscountAggregator = [];
124148

125149
/** @var \Magento\Quote\Model\Quote\Item $item */
@@ -300,6 +324,7 @@ private function aggregateDiscountPerRule(
300324
}
301325
}
302326
}
303-
$address->getExtensionAttributes()->setDiscounts(array_values($addressDiscountAggregator));
327+
328+
$address->getExtensionAttributes()->setDiscounts(array_values($addressDiscountAggregator));
304329
}
305330
}

0 commit comments

Comments
 (0)