@@ -111,43 +111,66 @@ public function __construct(
111
111
public function map (array $ item , int $ storeId ): OrderItem
112
112
{
113
113
$ product = $ this ->productIdentifier ->getProductByFacebookRetailerId ($ item ['retailer_id ' ]);
114
- $ pricePerUnit = $ item ['price_per_unit ' ]['amount ' ];
115
-
116
- $ originalPrice = $ this ->getPriceBeforeDiscount ($ item ['product_id ' ], $ storeId ) ?? $ pricePerUnit ;
114
+ $ productInfo = $ this ->getProductInfo ($ item ['product_id ' ], $ storeId );
117
115
118
116
$ quantity = $ item ['quantity ' ];
119
- $ taxAmount = $ item ['tax_details ' ]['estimated_tax ' ]['amount ' ];
120
-
121
- $ rowTotal = $ pricePerUnit * $ quantity ;
122
- $ promotionDetails = $ item ['promotion_details ' ]['data ' ] ?? null ;
123
- $ discountAmount = 0 ;
124
- if ($ promotionDetails ) {
125
- foreach ($ promotionDetails as $ promotionDetail ) {
126
- if ($ promotionDetail ['target_granularity ' ] === 'order_level ' ) {
127
- $ discountAmount += $ promotionDetail ['applied_amount ' ]['amount ' ];
128
- }
129
- }
130
- }
117
+
118
+ // strike-through price if available, otherwise list price
119
+ $ originalPrice = $ productInfo ['price ' ];
120
+
121
+ // sale price if available, otherwise list price
122
+ $ price = $ productInfo ['sale_price ' ] ?? $ originalPrice ;
123
+
124
+ // actual price, including applied discounts
125
+ $ discountPrice = $ item ['price_per_unit ' ]['amount ' ];
126
+
127
+ $ rowTotal = $ price * $ quantity ;
128
+ $ rowWeight = $ product ->getWeight () * $ quantity ;
129
+ $ rowTaxAmount = $ item ['tax_details ' ]['estimated_tax ' ]['amount ' ];
130
+ $ rowDiscountAmount = ($ price - $ discountPrice ) * $ quantity ;
131
+
132
+ $ discountAmount = $ price - $ discountPrice ;
133
+ $ discountPercent = round (($ discountAmount / $ price ) * 100 , 2 );
134
+
135
+ $ taxPercent = round ($ rowTaxAmount / ($ discountPrice * $ quantity ) * 100 , 2 );
136
+ $ priceInclTax = round (($ price * (100 + $ taxPercent ) / 100 ), 2 );
137
+ $ rowTotalInclTax = round (($ priceInclTax * $ quantity ), 2 );
138
+
139
+ // Dynamic Checkout:
140
+ // set applied_rule_ids
131
141
132
142
/** @var OrderItem $orderItem */
133
143
$ orderItem = $ this ->orderItemFactory ->create ();
134
- $ orderItem ->setProductId ($ product ->getId ())
144
+
145
+ $ orderItem
146
+ ->setProductId ($ product ->getId ())
135
147
->setSku ($ product ->getSku ())
136
148
->setName ($ product ->getName ())
137
149
->setQtyOrdered ($ quantity )
138
- ->setBasePrice ($ originalPrice )
139
150
->setOriginalPrice ($ originalPrice )
140
- ->setPrice ($ pricePerUnit )
141
- ->setTaxAmount ($ taxAmount )
151
+ ->setBaseOriginalPrice ($ originalPrice )
152
+ ->setPrice ($ price )
153
+ ->setBasePrice ($ price )
154
+ ->setPriceInclTax ($ priceInclTax )
155
+ ->setBasePriceInclTax ($ priceInclTax )
156
+ ->setTaxAmount ($ rowTaxAmount )
157
+ ->setBaseTaxAmount ($ rowTaxAmount )
158
+ ->setTaxPercent ($ taxPercent )
142
159
->setRowTotal ($ rowTotal )
143
- ->setDiscountAmount ($ discountAmount )
144
- ->setBaseDiscountAmount ($ discountAmount )
160
+ ->setBaseRowTotal ($ rowTotal )
161
+ ->setRowTotalInclTax ($ rowTotalInclTax )
162
+ ->setBaseRowTotalInclTax ($ rowTotalInclTax )
163
+ ->setRowWeight ($ rowWeight )
164
+ ->setDiscountAmount ($ rowDiscountAmount )
165
+ ->setBaseDiscountAmount ($ rowDiscountAmount )
166
+ ->setDiscountPercent ($ discountPercent )
145
167
->setProductType ($ product ->getTypeId ())
146
- ->setStoreId ($ storeId );
147
-
148
- if ($ rowTotal != 0 ) {
149
- $ orderItem ->setTaxPercent (round (($ taxAmount / $ rowTotal ) * 100 , 2 ));
150
- }
168
+ ->setWeight ($ product ->getWeight ())
169
+ ->setIsVirtual (false )
170
+ ->setIsQtyDecimal (false )
171
+ ->setStoreId ($ storeId )
172
+ ->setDiscountTaxCompensationAmount (0 )
173
+ ->setBaseDiscountTaxCompensationAmount (0 );
151
174
152
175
$ productOptions = $ this ->getProductOptions ($ product , $ orderItem );
153
176
if ($ productOptions ) {
@@ -209,26 +232,32 @@ private function getProductOptions(ProductInterface $product, OrderItem $orderIt
209
232
}
210
233
211
234
/**
212
- * Get price before discount from api loaded facebook product info
235
+ * Get product info for the provided product.
213
236
*
214
237
* @param string|int $fbProductId
215
238
* @param int $storeId
216
239
* @return string|bool
217
240
*/
218
- private function getPriceBeforeDiscount ($ fbProductId , int $ storeId )
241
+ private function getProductInfo ($ fbProductId , int $ storeId )
219
242
{
220
- try {
221
- $ this ->graphAPIAdapter
222
- ->setDebugMode ($ this ->systemConfig ->isDebugMode ($ storeId ))
223
- ->setAccessToken ($ this ->systemConfig ->getAccessToken ($ storeId ));
224
- $ productInfo = $ this ->graphAPIAdapter ->getProductInfo ($ fbProductId );
225
- if ($ productInfo && array_key_exists ('price ' , $ productInfo )) {
226
- //this returns amount without $, ex: $100.00 -> 100.00
227
- return substr ($ productInfo ['price ' ], 1 );
243
+ $ this ->graphAPIAdapter
244
+ ->setDebugMode ($ this ->systemConfig ->isDebugMode ($ storeId ))
245
+ ->setAccessToken ($ this ->systemConfig ->getAccessToken ($ storeId ));
246
+
247
+ $ productInfo = $ this ->graphAPIAdapter ->getProductInfo ($ fbProductId );
248
+
249
+ // strip the currency from the prices
250
+
251
+ if ($ productInfo ) {
252
+ if (array_key_exists ('price ' , $ productInfo )) {
253
+ $ productInfo ['price ' ] = substr ($ productInfo ['price ' ], 1 );
254
+ }
255
+
256
+ if (array_key_exists ('sale_price ' , $ productInfo )) {
257
+ $ productInfo ['sale_price ' ] = substr ($ productInfo ['sale_price ' ], 1 );
228
258
}
229
- } catch (GuzzleException $ e ) {
230
- $ this ->logger ->critical ($ e ->getMessage ());
231
259
}
232
- return false ;
260
+
261
+ return $ productInfo ;
233
262
}
234
263
}
0 commit comments