10
10
use Magento \Catalog \Api \Data \ProductTierPriceInterface ;
11
11
use Magento \Catalog \Api \Data \ProductTierPriceInterfaceFactory ;
12
12
use Magento \Catalog \Api \Data \ProductTierPriceExtensionFactory ;
13
- use Magento \Catalog \Model \ Product \ Price \ Validation \ TierPriceValidator ;
13
+ use Magento \Catalog \Api \ Data \ ProductInterface ;
14
14
use Magento \Framework \App \Config \ScopeConfigInterface ;
15
+ use Magento \Framework \Pricing \PriceCurrencyInterface ;
15
16
use Magento \Store \Model \ScopeInterface ;
16
17
use Magento \Store \Model \StoreManagerInterface ;
18
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
17
19
18
20
/**
19
21
* Builds ProductTierPriceInterface objects
@@ -23,12 +25,12 @@ class TierPriceBuilder
23
25
/**
24
26
* @var int
25
27
*/
26
- private $ websiteId = 0 ;
28
+ private $ websiteId ;
27
29
28
30
/**
29
31
* @var ProductTierPriceInterfaceFactory
30
32
*/
31
- protected $ tierPriceFactory ;
33
+ private $ tierPriceFactory ;
32
34
33
35
/**
34
36
* @var ProductTierPriceExtensionFactory
@@ -45,31 +47,80 @@ class TierPriceBuilder
45
47
*/
46
48
private $ storeManager ;
47
49
50
+ /**
51
+ * @var PriceCurrencyInterface
52
+ */
53
+ private $ priceCurrency ;
54
+
48
55
/**
49
56
* @param ProductTierPriceInterfaceFactory $tierPriceFactory
50
57
* @param ProductTierPriceExtensionFactory $tierPriceExtensionFactory
51
58
* @param ScopeConfigInterface $config
52
59
* @param StoreManagerInterface $storeManager
60
+ * @param PriceCurrencyInterface $priceCurrency
53
61
*/
54
62
public function __construct (
55
63
ProductTierPriceInterfaceFactory $ tierPriceFactory ,
56
64
ProductTierPriceExtensionFactory $ tierPriceExtensionFactory ,
57
65
ScopeConfigInterface $ config ,
58
- StoreManagerInterface $ storeManager
66
+ StoreManagerInterface $ storeManager ,
67
+ PriceCurrencyInterface $ priceCurrency
59
68
) {
60
69
$ this ->tierPriceFactory = $ tierPriceFactory ;
61
70
$ this ->tierPriceExtensionFactory = $ tierPriceExtensionFactory ;
62
71
$ this ->config = $ config ;
63
72
$ this ->storeManager = $ storeManager ;
73
+ $ this ->priceCurrency = $ priceCurrency ;
74
+
75
+ $ this ->setWebsiteId ();
76
+ }
77
+
78
+ /**
79
+ * Gets list of product tier prices
80
+ *
81
+ * @param ProductInterface $product
82
+ * @return ProductTierPriceInterface[]
83
+ */
84
+ public function getTierPrices ($ product )
85
+ {
86
+ /** @var array $tierPricesRaw */
87
+ $ tierPricesRaw = $ this ->loadData ($ product );
88
+
89
+ return $ this ->buildTierPriceObjects ($ tierPricesRaw );
90
+ }
91
+
92
+ /**
93
+ * Get tier data for a product
94
+ *
95
+ * @param ProductInterface $product
96
+ * @return array
97
+ */
98
+ private function loadData (ProductInterface $ product ): array
99
+ {
100
+ $ tierData = $ product ->getData (ProductAttributeInterface::CODE_TIER_PRICE );
101
+
102
+ if ($ tierData === null ) {
103
+ $ attribute = $ product ->getResource ()->getAttribute (ProductAttributeInterface::CODE_TIER_PRICE );
104
+ if ($ attribute ) {
105
+ $ attribute ->getBackend ()->afterLoad ($ product );
106
+ $ tierData = $ product ->getData (ProductAttributeInterface::CODE_TIER_PRICE );
107
+ }
108
+ }
109
+
110
+ if ($ tierData === null || !is_array ($ tierData )) {
111
+ return [];
112
+ }
113
+
114
+ return $ tierData ;
64
115
}
65
116
66
117
/**
67
- * Transform the raw tier prices of the product into array of ProductTierPriceInterface objects
118
+ * Transform the raw tier data into array of ProductTierPriceInterface objects
68
119
*
69
120
* @param array $tierPricesRaw
70
121
* @return ProductTierPriceInterface[]
71
122
*/
72
- public function buildTierPriceObjects (array $ tierPricesRaw ): array
123
+ private function buildTierPriceObjects (array $ tierPricesRaw ): array
73
124
{
74
125
$ prices = [];
75
126
@@ -88,9 +139,6 @@ public function buildTierPriceObjects(array $tierPricesRaw): array
88
139
*/
89
140
private function createTierPriceObjectFromRawData (array $ tierPriceRaw ): ProductTierPriceInterface
90
141
{
91
- //Find and set the website id that would be used as a fallback if the raw data does not bear it itself
92
- $ this ->setWebsiteForPriceScope ();
93
-
94
142
/** @var ProductTierPriceInterface $tierPrice */
95
143
$ tierPrice = $ this ->tierPriceFactory ->create ()
96
144
->setExtensionAttributes ($ this ->tierPriceExtensionFactory ->create ());
@@ -99,36 +147,63 @@ private function createTierPriceObjectFromRawData(array $tierPriceRaw): ProductT
99
147
isset ($ tierPriceRaw ['cust_group ' ]) ? $ tierPriceRaw ['cust_group ' ] : ''
100
148
);
101
149
$ tierPrice ->setValue (
102
- isset ($ tierPriceRaw[ ' website_price ' ]) ? $ tierPriceRaw [ ' website_price ' ] : $ tierPriceRaw [ ' price ' ]
150
+ $ this -> getPriceValue ($ tierPriceRaw)
103
151
);
104
152
$ tierPrice ->setQty (
105
153
isset ($ tierPriceRaw ['price_qty ' ]) ? $ tierPriceRaw ['price_qty ' ] : ''
106
154
);
107
155
$ tierPrice ->getExtensionAttributes ()->setWebsiteId (
108
- isset ($ tierPriceRaw ['website_id ' ]) ? ( int ) $ tierPriceRaw ['website_id ' ] : $ this ->websiteId
156
+ isset ($ tierPriceRaw ['website_id ' ]) ? $ tierPriceRaw ['website_id ' ] : $ this ->websiteId
109
157
);
110
158
if (isset ($ tierPriceRaw ['percentage_value ' ])) {
111
- $ tierPrice ->getExtensionAttributes ()->setPercentageValue ($ tierPriceRaw ['percentage_value ' ]);
159
+ $ tierPrice ->getExtensionAttributes ()->setPercentageValue (
160
+ $ tierPriceRaw ['percentage_value ' ]
161
+ );
112
162
}
113
163
114
164
return $ tierPrice ;
115
165
}
116
166
117
167
/**
118
- * Find and set the website id, based on the catalog price scope setting
168
+ * Get price value
169
+ *
170
+ * @param array $tierPriceRaw
171
+ * @return float
119
172
*/
120
- private function setWebsiteForPriceScope ()
173
+ private function getPriceValue ( array $ tierPriceRaw ): float
121
174
{
122
- if ($ this ->websiteId != 0 ) {
123
- return ;
175
+ $ valueInDefaultCurrency = $ this ->extractPriceValue ($ tierPriceRaw );
176
+ $ valueInStoreCurrency = $ this ->priceCurrency ->convertAndRound ($ valueInDefaultCurrency );
177
+
178
+ return $ valueInStoreCurrency ;
179
+ }
180
+
181
+ /**
182
+ * Extract float price value from raw data
183
+ *
184
+ * @param array $tierPriceRaw
185
+ * @return float
186
+ */
187
+ private function extractPriceValue (array $ tierPriceRaw ): float
188
+ {
189
+ if (isset ($ tierPriceRaw ['website_price ' ])) {
190
+ return (float )$ tierPriceRaw ['website_price ' ];
124
191
}
125
192
193
+ return (float )$ tierPriceRaw ['price ' ];
194
+ }
195
+
196
+ /**
197
+ * Find and set the website id
198
+ */
199
+ private function setWebsiteId ()
200
+ {
126
201
$ websiteId = 0 ;
127
202
$ value = $ this ->config ->getValue ('catalog/price/scope ' , ScopeInterface::SCOPE_WEBSITE );
128
203
if ($ value != 0 ) {
129
204
$ websiteId = $ this ->storeManager ->getWebsite ()->getId ();
130
205
}
131
206
132
- $ this ->websiteId = ( int ) $ websiteId ;
207
+ $ this ->websiteId = $ websiteId ;
133
208
}
134
209
}
0 commit comments