8
8
use Magento \Framework \App \ObjectManager ;
9
9
use Magento \SalesRule \Api \Data \RuleDiscountInterfaceFactory ;
10
10
use Magento \SalesRule \Api \Data \DiscountDataInterfaceFactory ;
11
+ use Magento \Quote \Api \Data \AddressExtensionFactory ;
11
12
12
13
/**
13
14
* Discount totals calculation model.
@@ -50,21 +51,28 @@ class Discount extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
50
51
*/
51
52
private $ discountDataInterfaceFactory ;
52
53
54
+ /**
55
+ * @var AddressExtensionFactory
56
+ */
57
+ private $ addressExtensionFactory ;
58
+
53
59
/**
54
60
* @param \Magento\Framework\Event\ManagerInterface $eventManager
55
61
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
56
62
* @param \Magento\SalesRule\Model\Validator $validator
57
63
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
58
64
* @param RuleDiscountInterfaceFactory|null $discountInterfaceFactory
59
65
* @param DiscountDataInterfaceFactory|null $discountDataInterfaceFactory
66
+ * @param AddressExtensionFactory|null $addressExtensionFactory
60
67
*/
61
68
public function __construct (
62
69
\Magento \Framework \Event \ManagerInterface $ eventManager ,
63
70
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
64
71
\Magento \SalesRule \Model \Validator $ validator ,
65
72
\Magento \Framework \Pricing \PriceCurrencyInterface $ priceCurrency ,
66
73
RuleDiscountInterfaceFactory $ discountInterfaceFactory = null ,
67
- DiscountDataInterfaceFactory $ discountDataInterfaceFactory = null
74
+ DiscountDataInterfaceFactory $ discountDataInterfaceFactory = null ,
75
+ AddressExtensionFactory $ addressExtensionFactory = null
68
76
) {
69
77
$ this ->setCode (self ::COLLECTOR_TYPE_CODE );
70
78
$ this ->eventManager = $ eventManager ;
@@ -75,6 +83,8 @@ public function __construct(
75
83
?: ObjectManager::getInstance ()->get (RuleDiscountInterfaceFactory::class);
76
84
$ this ->discountDataInterfaceFactory = $ discountDataInterfaceFactory
77
85
?: ObjectManager::getInstance ()->get (DiscountDataInterfaceFactory::class);
86
+ $ this ->addressExtensionFactory = $ addressExtensionFactory
87
+ ?: ObjectManager::getInstance ()->get (AddressExtensionFactory::class);
78
88
}
79
89
80
90
/**
@@ -84,6 +94,7 @@ public function __construct(
84
94
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
85
95
* @param \Magento\Quote\Model\Quote\Address\Total $total
86
96
* @return $this
97
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
87
98
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
88
99
* @SuppressWarnings(PHPMD.NPathComplexity)
89
100
*/
@@ -119,7 +130,20 @@ public function collect(
119
130
120
131
$ address ->setDiscountDescription ([]);
121
132
$ 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
+
123
147
$ addressDiscountAggregator = [];
124
148
125
149
/** @var \Magento\Quote\Model\Quote\Item $item */
@@ -300,6 +324,7 @@ private function aggregateDiscountPerRule(
300
324
}
301
325
}
302
326
}
303
- $ address ->getExtensionAttributes ()->setDiscounts (array_values ($ addressDiscountAggregator ));
327
+
328
+ $ address ->getExtensionAttributes ()->setDiscounts (array_values ($ addressDiscountAggregator ));
304
329
}
305
330
}
0 commit comments