Skip to content

Commit 49a31af

Browse files
Creating correct shipping option for magento orders (#453)
* Creating correct shipping option for magento orders * change * changes * changes * change * changes * changes * Removing explicit exception imports * changes * changes * changes * changes * changes * changes * changes * changes * changes * changes * changes * changes * chanmges * changes * a * changes * changes * cahnges * changes --------- Co-authored-by: Paul Kang <[email protected]>
1 parent 123cf77 commit 49a31af

File tree

6 files changed

+70
-5
lines changed

6 files changed

+70
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
use Magento\Framework\HTTP\Client\CurlFactory;
3434
use Magento\Framework\View\FileFactory;
3535

36+
/**
37+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38+
*/
3639
class GraphAPIAdapter
3740
{
3841
private const ORDER_STATE_CREATED = 'CREATED';
@@ -580,7 +583,7 @@ public function getOrders($pageId, $cursorAfter = false, $filterType = "")
580583
'estimated_payment_details',
581584
'ship_by_date',
582585
'order_status',
583-
'selected_shipping_option',
586+
'selected_shipping_option{name, reference_id, price, calculated_tax, estimated_shipping_time}',
584587
'shipping_address{first_name, last_name, street1, street2, city, postal_code, country}',
585588
'payments',
586589
'promotion_details{applied_amount, coupon_code, target_granularity, sponsor, campaign_name}',

app/code/Meta/BusinessExtension/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"magento/module-eav": "*",
1414
"magento/module-security": "*",
1515
"magento/module-store": "*",
16-
"magento/module-webapi": "*",
1716
"facebook/php-business-sdk": "^15.0.0"
1817
},
1918
"autoload": {

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
use Magento\Store\Model\StoreManagerInterface;
3232
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
3333
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
34+
use Meta\Sales\Plugin\ShippingMethodTypes;
3435

3536
/**
3637
* Map facebook order data to magento order
38+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3739
*/
3840
class OrderMapper
3941
{
@@ -120,6 +122,7 @@ public function map(array $data, int $storeId): Order
120122

121123
$channel = ucfirst($data['channel']);
122124
$shippingOptionName = $data['selected_shipping_option']['name'];
125+
$shippingReferenceId = $data['selected_shipping_option']['reference_id'];
123126
$billingAddress = $this->getOrderBillingAddress($data);
124127
$shippingAddress = clone $billingAddress;
125128
$shippingAddress
@@ -146,7 +149,7 @@ public function map(array $data, int $storeId): Order
146149

147150
$this->applyTotalsToOrder($order, $data, $storeId);
148151

149-
$shippingMethod = $this->getShippingMethod($shippingOptionName, $storeId);
152+
$shippingMethod = $this->getShippingMethod($shippingOptionName, $shippingReferenceId, $storeId);
150153
$shippingDescription = $this->getShippingMethodLabel($shippingOptionName, $storeId);
151154

152155
$order->setStoreId($storeId)
@@ -172,12 +175,19 @@ public function map(array $data, int $storeId): Order
172175
* Get Magento shipping method code. For example: "flatrate_flatrate"
173176
*
174177
* @param string $shippingOptionName (possible values: "standard", "expedited", "rush")
178+
* @param string $shippingReferenceId
175179
* @param int $storeId
176180
* @return string|null
177181
* @throws LocalizedException
178182
*/
179-
private function getShippingMethod(string $shippingOptionName, int $storeId): ?string
183+
private function getShippingMethod(string $shippingOptionName, string $shippingReferenceId, int $storeId): ?string
180184
{
185+
$static_shipping_options = [ShippingMethodTypes::FREE_SHIPPING,
186+
ShippingMethodTypes::FLAT_RATE,
187+
ShippingMethodTypes::TABLE_RATE];
188+
if (in_array($shippingReferenceId, $static_shipping_options)) {
189+
return $shippingReferenceId;
190+
}
181191
$map = $this->systemConfig->getShippingMethodsMap($storeId);
182192
foreach (['standard', 'expedited', 'rush'] as $item) {
183193
if (stripos($shippingOptionName, $item) !== false && isset($map[$item])) {

app/code/Meta/Sales/Plugin/ShippingData.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class ShippingData
4949
public const ATTR_SHIPPING_FEE_TYPE = 'shipping_fee_type';
5050
public const ATTR_FREE_SHIPPING_MIN_ORDER_AMOUNT = 'free_shipping_minimum_order_amount';
5151

52+
public const EXTERNAL_REFERENCE_ID = 'external_reference_id';
53+
5254
/**
5355
* @param CollectionFactory $tableRateCollection
5456
* @param ScopeConfigInterface $scopeConfig
@@ -104,6 +106,7 @@ public function buildShippingProfile(string $shippingProfileType): array
104106
$freeShippingThreshold = $this->getFieldFromModel($shippingProfileType, 'free_shipping_subtotal');
105107
$handlingFee = $this->getFieldFromModel($shippingProfileType, 'handling_fee');
106108
$handlingFeeType = $this->getFieldFromModel($shippingProfileType, 'handling_type');
109+
$externalReferenceId = $this->getExternalReferenceID($shippingProfileType);
107110
return [
108111
self::ATTR_ENABLED => $isEnabled,
109112
self::ATTR_TITLE => $title,
@@ -113,6 +116,7 @@ public function buildShippingProfile(string $shippingProfileType): array
113116
self::ATTR_HANDLING_FEE_TYPE => $handlingFeeType,
114117
self::ATTR_SHIPPING_FEE_TYPE => $shippingType,
115118
self::ATTR_FREE_SHIPPING_MIN_ORDER_AMOUNT => $freeShippingThreshold,
119+
self::EXTERNAL_REFERENCE_ID => $externalReferenceId
116120
];
117121
}
118122

@@ -129,6 +133,26 @@ private function getFieldFromModel(string $shippingProfileType, string $field)
129133
return $this->scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->storeId);
130134
}
131135

136+
/**
137+
* Get external reference id for shipping settings which meta will use to identify the selected shipping option
138+
*
139+
* @param string $shippingProfileType
140+
* @return string
141+
*/
142+
private function getExternalReferenceID(string $shippingProfileType): string
143+
{
144+
switch ($shippingProfileType) {
145+
case ShippingProfileTypes::TABLE_RATE:
146+
return ShippingMethodTypes::TABLE_RATE;
147+
case ShippingProfileTypes::FLAT_RATE:
148+
return ShippingMethodTypes::FLAT_RATE;
149+
case ShippingProfileTypes::FREE_SHIPPING:
150+
return ShippingMethodTypes::FREE_SHIPPING;
151+
default:
152+
return "";
153+
}
154+
}
155+
132156
/**
133157
* A function that builds shipping methods info for shipping profiles which are not table rates
134158
*

app/code/Meta/Sales/Plugin/ShippingFileBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public function getHeaderFields(): array
7878
ShippingData::ATTR_HANDLING_FEE,
7979
ShippingData::ATTR_HANDLING_FEE_TYPE,
8080
ShippingData::ATTR_SHIPPING_FEE_TYPE,
81-
ShippingData::ATTR_FREE_SHIPPING_MIN_ORDER_AMOUNT
81+
ShippingData::ATTR_FREE_SHIPPING_MIN_ORDER_AMOUNT,
82+
ShippingData::EXTERNAL_REFERENCE_ID
8283
];
8384
}
8485
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Plugin;
22+
23+
class ShippingMethodTypes
24+
{
25+
public const TABLE_RATE = 'tablerate_bestway';
26+
public const FREE_SHIPPING = 'freeshipping_freeshipping';
27+
public const FLAT_RATE = 'flatrate_flatrate';
28+
}

0 commit comments

Comments
 (0)