Skip to content

Commit d551c98

Browse files
committed
Merge branch '1.2.4-rc' of https://github.com/magento-commerce/facebook-for-magento2 into 1.2.4
2 parents 38c6d9d + 5d1da72 commit d551c98

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
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, target_type, sponsor, campaign_name}',
588+
'promotion_details{applied_amount, coupon_code, target_granularity, sponsor, campaign_name}',
589589
'last_updated',
590590
];
591591
$request = [

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,30 @@ public function map(array $item, int $storeId): OrderItem
121121
// sale price if available, otherwise list price
122122
$price = $productInfo['sale_price'] ?? $originalPrice;
123123

124-
// actual price, including applied discounts
124+
// actual price, including applied item-level discounts
125125
$discountPrice = $item['price_per_unit']['amount'];
126126

127+
$discountAmountItems = $price - $discountPrice;
128+
$discountPercent = round(($discountAmountItems / $price) * 100, 2);
129+
130+
$discountAmountOrder = 0;
131+
$promotionDetails = $item['promotion_details']['data'] ?? null;
132+
if ($promotionDetails) {
133+
foreach ($promotionDetails as $promotionDetail) {
134+
if ($promotionDetail['target_granularity'] === 'order_level') {
135+
$discountAmountOrder += $promotionDetail['applied_amount']['amount'];
136+
}
137+
}
138+
}
139+
127140
$rowTotal = $price * $quantity;
128141
$rowWeight = $product->getWeight() * $quantity;
129142
$rowTaxAmount = $item['tax_details']['estimated_tax']['amount'];
130-
$rowDiscountAmount = ($price - $discountPrice) * $quantity;
131-
132-
$discountAmount = $price - $discountPrice;
133-
$discountPercent = round(($discountAmount / $price) * 100, 2);
143+
$rowDiscountAmount = round(($discountAmountItems * $quantity) + $discountAmountOrder, 2);
144+
$rowDiscountPrice = $discountPrice * $quantity - $discountAmountOrder;
134145

135-
$taxPercent = round($rowTaxAmount / ($discountPrice * $quantity) * 100, 2);
146+
// discount price can be $0 for free BXGY items
147+
$taxPercent = $rowDiscountPrice > 0 ? round(($rowTaxAmount / $rowDiscountPrice) * 100, 2) : 0;
136148
$priceInclTax = round(($price * (100 + $taxPercent) / 100), 2);
137149
$rowTotalInclTax = round(($priceInclTax * $quantity), 2);
138150

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ public function map(array $data, int $storeId): Order
170170

171171
$this->applyShippingToOrder($order, $data, $storeId);
172172
$this->applyItemsToOrder($order, $data, $storeId);
173-
$this->applyDiscountsToOrder($order, $data);
174173
$this->applyTotalsToOrder($order, $data, $storeId);
175174

176175
$order->addCommentToStatusHistory("Order Imported from Meta. Meta Order ID: #{$facebookOrderId}");
@@ -315,7 +314,7 @@ private function getOrderBillingAddress(array $data): Order\Address
315314
'street' => $street,
316315
'city' => $data['shipping_address']['city'],
317316
'email' => $data['buyer_details']['email'],
318-
'telephone' => '0', // is required by magento
317+
'telephone' => 'N/A', // is required by magento
319318
'country_id' => $data['shipping_address']['country'] // maps 1:1
320319
];
321320

@@ -331,25 +330,29 @@ private function getOrderBillingAddress(array $data): Order\Address
331330
*
332331
* @param Order $order
333332
* @param array $data
333+
* @param array $items
334334
* @return void
335335
*/
336-
private function applyDiscountsToOrder(Order $order, array $data)
336+
private function applyDiscountsToOrder(Order $order, array $data, array $items)
337337
{
338-
$promotionDetails = $data['promotion_details'] ?? null;
339-
$orderSubtotalAmount = $data['estimated_payment_details']['subtotal']['items']['amount'];
340338
$discountAmount = 0;
341339
$discountNames = [];
342340

343-
if ($promotionDetails) {
344-
foreach ($promotionDetails['data'] as $promotionDetail) {
345-
$targetType = $promotionDetail['target_type'] ?? null;
346-
if ($targetType === 'shipping') {
347-
// don't treat free shipping as a discount since it is
348-
// already reflected as free under shipping charges.
349-
} else {
341+
// calculate discounts using items to exclude order-level shipping discounts
342+
foreach ($items['data'] as $item) {
343+
$itemPromotionDetails = $item['promotion_details']['data'] ?? null;
344+
if ($itemPromotionDetails) {
345+
foreach ($itemPromotionDetails as $promotionDetail) {
350346
$discountAmount -= $promotionDetail['applied_amount']['amount'];
351347
}
348+
}
349+
}
350+
351+
$orderPromotionDetails = $data['promotion_details'] ?? null;
352+
$orderSubtotalAmount = $data['estimated_payment_details']['subtotal']['items']['amount'];
352353

354+
if ($orderPromotionDetails) {
355+
foreach ($orderPromotionDetails['data'] as $promotionDetail) {
353356
$couponCode = $promotionDetail['coupon_code'] ?? null;
354357
if ($couponCode) {
355358
$order->setCouponCode($couponCode);
@@ -417,6 +420,8 @@ private function applyItemsToOrder(Order $order, array $data, int $storeId)
417420
->setBaseSubtotalInclTax($subtotalInclTax)
418421
->setTotalQtyOrdered($totalQtyOrdered)
419422
->setWeight($weight);
423+
424+
$this->applyDiscountsToOrder($order, $data, $items);
420425
}
421426

422427
/**

0 commit comments

Comments
 (0)