Skip to content

Commit a1412c8

Browse files
strygoswinard-meta
andauthored
Orders: Add Missing Fields (#554)
* Initial commit * bug fixes * discount fixes * Refactor item mapping into separate function * Rely on new target_type field to properly filter shipping * Refactor shipping into its own method * Fix typo * Fix typo * Update app/code/Meta/Sales/Model/Mapper/OrderItemMapper.php Co-authored-by: swinard-meta <[email protected]> * Add comments * Removed flag after confirming Magento behavior --------- Co-authored-by: swinard-meta <[email protected]>
1 parent e1e585d commit a1412c8

File tree

3 files changed

+200
-92
lines changed

3 files changed

+200
-92
lines changed

app/code/Meta/BusinessExtension/Helper/GraphAPIAdapter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ public function getOrders($ordersRootId, $cursorAfter = false, $filterType = "")
585585
'selected_shipping_option{name, reference_id, price, calculated_tax, estimated_shipping_time}',
586586
'shipping_address{first_name, last_name, street1, street2, city, postal_code, state, country}',
587587
'payments',
588-
'promotion_details{applied_amount, coupon_code, target_granularity, sponsor, campaign_name}',
588+
'promotion_details{applied_amount, coupon_code, target_granularity, target_type, sponsor, campaign_name}',
589589
'last_updated',
590590
];
591591
$request = [
@@ -829,7 +829,8 @@ public function refundOrder(
829829
public function getProductInfo($fbProductId)
830830
{
831831
$requestFields = [
832-
'price'
832+
'price',
833+
'sale_price'
833834
];
834835

835836
$request = [

app/code/Meta/Sales/Model/Mapper/OrderItemMapper.php

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -111,43 +111,66 @@ public function __construct(
111111
public function map(array $item, int $storeId): OrderItem
112112
{
113113
$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);
117115

118116
$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
131141

132142
/** @var OrderItem $orderItem */
133143
$orderItem = $this->orderItemFactory->create();
134-
$orderItem->setProductId($product->getId())
144+
145+
$orderItem
146+
->setProductId($product->getId())
135147
->setSku($product->getSku())
136148
->setName($product->getName())
137149
->setQtyOrdered($quantity)
138-
->setBasePrice($originalPrice)
139150
->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)
142159
->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)
145167
->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);
151174

152175
$productOptions = $this->getProductOptions($product, $orderItem);
153176
if ($productOptions) {
@@ -209,26 +232,32 @@ private function getProductOptions(ProductInterface $product, OrderItem $orderIt
209232
}
210233

211234
/**
212-
* Get price before discount from api loaded facebook product info
235+
* Get product info for the provided product.
213236
*
214237
* @param string|int $fbProductId
215238
* @param int $storeId
216239
* @return string|bool
217240
*/
218-
private function getPriceBeforeDiscount($fbProductId, int $storeId)
241+
private function getProductInfo($fbProductId, int $storeId)
219242
{
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);
228258
}
229-
} catch (GuzzleException $e) {
230-
$this->logger->critical($e->getMessage());
231259
}
232-
return false;
260+
261+
return $productInfo;
233262
}
234263
}

0 commit comments

Comments
 (0)