Skip to content

Commit 17bd627

Browse files
authored
Adding logic to convert state names to full name (#501)
* Adding logic to convert state names to full name * Pivoting to using Magento's inbuilt region resolver * As suggested in PR feedback, moving state to fullname function to Shipping helper * correcting the accessor function for full name * Redundant else case * addressing PHPDoc convention
1 parent fdb3329 commit 17bd627

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

app/code/Meta/Sales/Helper/ShippingHelper.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class ShippingHelper extends AbstractHelper
3737
* @param array $supportedShippingCarriers
3838
*/
3939
public function __construct(
40-
Context $context,
41-
RegionFactory $regionFactory,
40+
Context $context,
41+
RegionFactory $regionFactory,
4242
LoggerInterface $logger,
43-
array $supportedShippingCarriers = []
43+
array $supportedShippingCarriers = []
4444
) {
4545
parent::__construct($context);
4646
$this->regionFactory = $regionFactory;
@@ -78,6 +78,26 @@ public function getRegionName($stateId)
7878
return $stateId;
7979
}
8080

81+
/**
82+
* Gets the region name from state code
83+
*
84+
* @param null|string $stateCode - State code
85+
* @param null|string $countryCode - Country code
86+
* @return null|string
87+
*/
88+
public function getRegionNameFromCode(?string $stateCode, ?string $countryCode): ?string
89+
{
90+
$regionName = $stateCode ?? null;
91+
try {
92+
$region = $this->regionFactory->create();
93+
$region = $region->loadByCode($stateCode, $countryCode);
94+
$regionName = $region->getDefaultName() ?? $regionName;
95+
} catch (Exception $e) {
96+
$this->logger->critical($e->getMessage());
97+
}
98+
return $regionName;
99+
}
100+
81101
/**
82102
* A map for popular US carriers with long titles
83103
*
@@ -86,8 +106,8 @@ public function getRegionName($stateId)
86106
private function getSupplementaryCarriersMap()
87107
{
88108
return [
89-
'UPS' => 'United Parcel Service',
90-
'USPS' => 'United States Postal Service',
109+
'UPS' => 'United Parcel Service',
110+
'USPS' => 'United States Postal Service',
91111
'FEDEX' => 'Federal Express',
92112
];
93113
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
3333
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
3434
use Meta\Sales\Plugin\ShippingMethodTypes;
35+
use Meta\Sales\Helper\ShippingHelper;
3536

3637
/**
3738
* Map facebook order data to magento order
@@ -74,6 +75,11 @@ class OrderMapper
7475
*/
7576
private OrderItemMapper $orderItemMapper;
7677

78+
/**
79+
* @var ShippingHelper
80+
*/
81+
private ShippingHelper $shippingHelper;
82+
7783
/**
7884
* @param StoreManagerInterface $storeManager
7985
* @param GraphAPIAdapter $graphAPIAdapter
@@ -82,6 +88,7 @@ class OrderMapper
8288
* @param OrderPaymentInterfaceFactory $paymentFactory
8389
* @param OrderAddressInterfaceFactory $orderAddressFactory
8490
* @param OrderItemMapper $orderItemMapper
91+
* @param ShippingHelper $shippingHelper
8592
*/
8693
public function __construct(
8794
StoreManagerInterface $storeManager,
@@ -90,7 +97,8 @@ public function __construct(
9097
OrderInterfaceFactory $orderFactory,
9198
OrderPaymentInterfaceFactory $paymentFactory,
9299
OrderAddressInterfaceFactory $orderAddressFactory,
93-
OrderItemMapper $orderItemMapper
100+
OrderItemMapper $orderItemMapper,
101+
ShippingHelper $shippingHelper
94102
) {
95103
$this->storeManager = $storeManager;
96104
$this->graphAPIAdapter = $graphAPIAdapter;
@@ -99,6 +107,7 @@ public function __construct(
99107
$this->paymentFactory = $paymentFactory;
100108
$this->orderAddressFactory = $orderAddressFactory;
101109
$this->orderItemMapper = $orderItemMapper;
110+
$this->shippingHelper = $shippingHelper;
102111
}
103112

104113
/**
@@ -183,8 +192,8 @@ public function map(array $data, int $storeId): Order
183192
private function getShippingMethod(string $shippingOptionName, string $shippingReferenceId, int $storeId): ?string
184193
{
185194
$static_shipping_options = [ShippingMethodTypes::FREE_SHIPPING,
186-
ShippingMethodTypes::FLAT_RATE,
187-
ShippingMethodTypes::TABLE_RATE];
195+
ShippingMethodTypes::FLAT_RATE,
196+
ShippingMethodTypes::TABLE_RATE];
188197
if (in_array($shippingReferenceId, $static_shipping_options)) {
189198
return $shippingReferenceId;
190199
}
@@ -229,8 +238,13 @@ private function getOrderBillingAddress(array $data): Order\Address
229238
? [$data['shipping_address']['street1'], $data['shipping_address']['street2']]
230239
: $data['shipping_address']['street1'];
231240

241+
$regionName = $this->shippingHelper->getRegionNameFromCode(
242+
$data['shipping_address']['state'],
243+
$data['shipping_address']['country']
244+
);
245+
232246
$addressData = [
233-
'region' => $data['shipping_address']['state'] ?? null,
247+
'region' => $regionName,
234248
'postcode' => $data['shipping_address']['postal_code'],
235249
'firstname' => $data['shipping_address']['first_name'],
236250
'lastname' => $data['shipping_address']['last_name'],

0 commit comments

Comments
 (0)