Skip to content

Commit d844aaa

Browse files
danielmetaconnollysinghramanpreettsol-loup
authored
Displaying Title/Method name under shipping information in Magento orders tab, instead of Title/method code (#496)
* ordermapper * deleting things * changing * changes * changes * changes * changes * changes * changes * changes * changes * changes * changes * change * Category collection class split into 2 small files * Comments addressed * supress warning added * commandeering PR and addressing feedback * Change arbitrary delimiter --------- Co-authored-by: singhramanpreett <[email protected]> Co-authored-by: Paul Kang <[email protected]>
1 parent 14eb676 commit d844aaa

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

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

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
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\ShippingData;
3435
use Meta\Sales\Plugin\ShippingMethodTypes;
3536
use Meta\Sales\Helper\ShippingHelper;
3637

@@ -75,6 +76,11 @@ class OrderMapper
7576
*/
7677
private OrderItemMapper $orderItemMapper;
7778

79+
/**
80+
* @var ShippingData
81+
*/
82+
private ShippingData $shippingData;
83+
7884
/**
7985
* @var ShippingHelper
8086
*/
@@ -88,6 +94,7 @@ class OrderMapper
8894
* @param OrderPaymentInterfaceFactory $paymentFactory
8995
* @param OrderAddressInterfaceFactory $orderAddressFactory
9096
* @param OrderItemMapper $orderItemMapper
97+
* @param ShippingData $shippingData
9198
* @param ShippingHelper $shippingHelper
9299
*/
93100
public function __construct(
@@ -98,6 +105,7 @@ public function __construct(
98105
OrderPaymentInterfaceFactory $paymentFactory,
99106
OrderAddressInterfaceFactory $orderAddressFactory,
100107
OrderItemMapper $orderItemMapper,
108+
ShippingData $shippingData,
101109
ShippingHelper $shippingHelper
102110
) {
103111
$this->storeManager = $storeManager;
@@ -107,6 +115,7 @@ public function __construct(
107115
$this->paymentFactory = $paymentFactory;
108116
$this->orderAddressFactory = $orderAddressFactory;
109117
$this->orderItemMapper = $orderItemMapper;
118+
$this->shippingData = $shippingData;
110119
$this->shippingHelper = $shippingHelper;
111120
}
112121

@@ -130,8 +139,8 @@ public function map(array $data, int $storeId): Order
130139
->setAccessToken($accessToken);
131140

132141
$channel = ucfirst($data['channel']);
133-
$shippingOptionName = $data['selected_shipping_option']['name'];
134-
$shippingReferenceId = $data['selected_shipping_option']['reference_id'];
142+
$metaShippingOptionName = $data['selected_shipping_option']['name'];
143+
$magentoShippingReferenceID = $data['selected_shipping_option']['reference_id'];
135144
$billingAddress = $this->getOrderBillingAddress($data);
136145
$shippingAddress = clone $billingAddress;
137146
$shippingAddress
@@ -158,13 +167,16 @@ public function map(array $data, int $storeId): Order
158167

159168
$this->applyTotalsToOrder($order, $data, $storeId);
160169

161-
$shippingMethod = $this->getShippingMethod($shippingOptionName, $shippingReferenceId, $storeId);
162-
$shippingDescription = $this->getShippingMethodLabel($shippingOptionName, $storeId);
170+
$shippingMethod = $this->getShippingMethod($metaShippingOptionName, $magentoShippingReferenceID, $storeId);
171+
$shippingDescription = $this->getShippingDescription($metaShippingOptionName, $shippingMethod, $storeId);
172+
// This should never happen, as it means Meta has passed a shipping method with no equivalent in Magento.
173+
// @todo strictly handle this edge case by canceling the entire Meta order if this happens.
174+
$fallbackShippingDescription = $metaShippingOptionName . " - {$shippingMethod}";
163175

164176
$order->setStoreId($storeId)
165177
// @todo have to set shipping method like this
166178
->setShippingMethod($shippingMethod)
167-
->setShippingDescription($shippingDescription ?? $shippingOptionName . " / {$shippingMethod}")
179+
->setShippingDescription($shippingDescription ?? $fallbackShippingDescription)
168180
->setPayment($payment);
169181

170182
// @todo implement paging and tax for order items
@@ -191,10 +203,7 @@ public function map(array $data, int $storeId): Order
191203
*/
192204
private function getShippingMethod(string $shippingOptionName, string $shippingReferenceId, int $storeId): ?string
193205
{
194-
$static_shipping_options = [ShippingMethodTypes::FREE_SHIPPING,
195-
ShippingMethodTypes::FLAT_RATE,
196-
ShippingMethodTypes::TABLE_RATE];
197-
if (in_array($shippingReferenceId, $static_shipping_options)) {
206+
if (in_array($shippingReferenceId, $this->getSyncableShippingMethodTypes())) {
198207
return $shippingReferenceId;
199208
}
200209
$map = $this->systemConfig->getShippingMethodsMap($storeId);
@@ -226,6 +235,49 @@ private function getShippingMethodLabel(string $shippingOptionName, int $storeId
226235
return null;
227236
}
228237

238+
/**
239+
* Get ShippingMethodDescription
240+
*
241+
* @param string $metaShippingTitle
242+
* @param string $shippingMethod
243+
* @param int $storeId
244+
* @return string|null
245+
*/
246+
private function getShippingDescription(string $metaShippingTitle, string $shippingMethod, int $storeId): ?string
247+
{
248+
$shippingLabel = $this->getShippingMethodLabel($metaShippingTitle, $storeId);
249+
if ($shippingLabel) {
250+
return $shippingLabel;
251+
}
252+
253+
if (in_array($shippingMethod, $this->getSyncableShippingMethodTypes())) {
254+
$this->shippingData->setStoreId($storeId);
255+
[$carrier] = explode('_', $shippingMethod);
256+
// Possible values are string, '' and null. Falsey check is acceptable here.
257+
if ($carrier) {
258+
$shippingMethodName = $this->shippingData->getFieldFromModel($carrier, 'name');
259+
$shippingOptionTitle = $this->shippingData->getFieldFromModel($carrier, 'title');
260+
return $shippingOptionTitle . ' - ' . $shippingMethodName;
261+
}
262+
}
263+
264+
return null;
265+
}
266+
267+
/**
268+
* This function returns a list of shipping methods that can be synced to Meta
269+
*
270+
* @return array
271+
*/
272+
public function getSyncableShippingMethodTypes(): array
273+
{
274+
return [
275+
ShippingMethodTypes::FREE_SHIPPING,
276+
ShippingMethodTypes::FLAT_RATE,
277+
ShippingMethodTypes::TABLE_RATE
278+
];
279+
}
280+
229281
/**
230282
* Create a magento order billing address from facebook order data
231283
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function buildShippingProfile(string $shippingProfileType): array
128128
* @param string $field
129129
* @return mixed
130130
*/
131-
private function getFieldFromModel(string $shippingProfileType, string $field)
131+
public function getFieldFromModel(string $shippingProfileType, string $field)
132132
{
133133
$path = 'carriers/' . $shippingProfileType . '/' . $field;
134134
return $this->scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->storeId);

0 commit comments

Comments
 (0)