Skip to content

Commit fd2bebc

Browse files
committed
Merge branch 'main' into 1.2.2-rc
2 parents 88f42b1 + 0a3893d commit fd2bebc

File tree

7 files changed

+118
-25
lines changed

7 files changed

+118
-25
lines changed

app/code/Meta/BusinessExtension/Model/System/Config.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,21 @@ public function getAppId(): string
207207
*/
208208
public function getModuleVersion(): string
209209
{
210-
$this->version = (string)($this->version ?: $this->cache->load(self::VERSION_CACHE_KEY));
211-
if (!$this->version) {
212-
$installedPackages = $this->composerInformation->getInstalledMagentoPackages();
213-
$extensionVersion = $installedPackages[self::EXTENSION_PACKAGE_NAME]['version'] ?? null;
214-
if (!empty($extensionVersion)) {
215-
$this->version = $extensionVersion;
216-
} else {
217-
$this->version = 'dev';
218-
}
219-
$this->cache->save($this->version, self::VERSION_CACHE_KEY, [AppConfig::CACHE_TAG]);
210+
$cachedVersion = $this->cache->load(self::VERSION_CACHE_KEY);
211+
if ($cachedVersion) {
212+
// Once we've calculated the version for this session, default to the cached value.
213+
return $cachedVersion;
214+
}
215+
$installedPackages = $this->composerInformation->getInstalledMagentoPackages();
216+
// We are now setting the "DEV" version locally as well as via composer to facilitate logging.
217+
// If there is ever a conflict, we should prefer Composer's value.
218+
$officialExtensionVersion = $installedPackages[self::EXTENSION_PACKAGE_NAME]['version'] ?? null;
219+
if ($officialExtensionVersion) {
220+
$this->version = $officialExtensionVersion;
221+
} else {
222+
$this->version = (string)($this->version ?: 'dev');
220223
}
224+
$this->cache->save($this->version, self::VERSION_CACHE_KEY, [AppConfig::CACHE_TAG]);
221225
return $this->version;
222226
}
223227

app/code/Meta/BusinessExtension/etc/adminhtml/di.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
</argument>
1010
</arguments>
1111
</type>
12-
<preference for="Magento\Framework\App\Config\Storage\WriterInterface"
13-
type="Magento\Framework\App\Config\Storage\Writer"/>
1412
<type name="Meta\BusinessExtension\Controller\Adminhtml\ApiKey\Index">
1513
<arguments>
1614
<argument name="configWriter" xsi:type="object">Magento\Framework\App\Config\Storage\WriterInterface

app/code/Meta/BusinessExtension/etc/di.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
</argument>
3535
</arguments>
3636
</type>
37-
<preference for="Magento\Framework\App\Config\Storage\WriterInterface"
38-
type="Magento\Framework\App\Config\Storage\Writer"/>
3937
<type name="Meta\BusinessExtension\Controller\Adminhtml\ApiKey\Index">
4038
<arguments>
4139
<argument name="configWriter" xsi:type="object">Magento\Framework\App\Config\Storage\WriterInterface;

app/code/Meta/Catalog/Cron/LogCatalogSetup.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ public function __construct(
8686
public function execute()
8787
{
8888
foreach ($this->systemConfig->getAllFBEInstalledStores() as $store) {
89+
$storeId = $store->getId();
8990
try {
90-
$storeId = $store->getId();
91+
$accessToken = $this->systemConfig->getAccessToken($storeId);
92+
if (!$accessToken) {
93+
continue;
94+
}
95+
9196
$this->graphAPIAdapter->persistLogToMeta([
9297
'event' => 'log_catalog_setup_data',
9398
'event_type' => 'log_catalog_setup',
@@ -108,7 +113,7 @@ public function execute()
108113
]);
109114
} catch (\Exception $e) {
110115
$this->fbeHelper->logExceptionImmediatelyToMeta($e, [
111-
'catalog_id' => $this->systemConfig->getCatalogId(),
116+
'store_id' => $storeId,
112117
'event' => 'log_catalog_setup_cron_exception',
113118
'event_type' => 'log_catalog_setup'
114119
]);

app/code/Meta/Promotions/Model/Promotion/Feed/Builder.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
namespace Meta\Promotions\Model\Promotion\Feed;
2222

23+
use Magento\SalesRule\Api\Data\CouponInterface;
24+
use Magento\SalesRule\Model\Coupon;
2325
use Meta\BusinessExtension\Helper\FBEHelper;
2426
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2527
use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollection;
@@ -53,6 +55,7 @@ class Builder
5355
private const ATTR_USE_AUTO_GENERATION = 'use_auto_generation';
5456
private const ATTR_USES_PER_COUPON = 'uses_per_coupon';
5557
private const ATTR_PRIMARY_COUPON = 'primary_coupon';
58+
private const ATTR_COUPONS = 'coupons';
5659
private const ATTR_SIMPLE_FREE_SHIPPING = 'simple_free_shipping';
5760
private const ATTR_STORE_LABELS = 'store_labels';
5861
private const ATTR_WEBSITE_IDS = 'website_ids';
@@ -158,6 +161,7 @@ public function buildPromoEntry(Rule $rule): array
158161
self::ATTR_USE_AUTO_GENERATION => $useAutoGeneration,
159162
self::ATTR_USES_PER_COUPON => $rule->getUsesPerCoupon(),
160163
self::ATTR_PRIMARY_COUPON => $rule->getCouponCode(),
164+
self::ATTR_COUPONS => $this->getAllCouponsSerialized($rule),
161165
self::ATTR_SIMPLE_FREE_SHIPPING => $this->getSimpleFreeShippingAsString($rule),
162166
self::ATTR_STORE_LABELS => json_encode($rule->getStoreLabels()),
163167
self::ATTR_WEBSITE_IDS => json_encode($rule->getWebsiteIds()),
@@ -216,6 +220,55 @@ private function getSimpleFreeShippingAsString(Rule $rule): string
216220
}
217221
}
218222

223+
/**
224+
* Get coupon creation type string
225+
*
226+
* @param Coupon $coupon
227+
* @return string
228+
*/
229+
private function getCouponCreationTypeAsString(Coupon $coupon): string
230+
{
231+
$type = $coupon->getType();
232+
switch ($type) {
233+
case CouponInterface::TYPE_MANUAL:
234+
return 'type_manual';
235+
case CouponInterface::TYPE_GENERATED:
236+
return 'type_generated';
237+
default:
238+
return 'type_unknown';
239+
}
240+
}
241+
242+
/**
243+
* Get all coupons
244+
*
245+
* @param Rule $rule
246+
* @return string
247+
*/
248+
private function getAllCouponsSerialized(Rule $rule): string
249+
{
250+
$coupons = $rule->getCoupons();
251+
array_unshift($coupons, $rule->getPrimaryCoupon());
252+
253+
$coupons = array_map(
254+
function (Coupon $coupon): array {
255+
return [
256+
'id' => $coupon->getCouponId(),
257+
'code' => $coupon->getCode(),
258+
'usage_limit' => $coupon->getUsageLimit(),
259+
'usage_per_customer' => $coupon->getUsagePerCustomer(),
260+
'times_used' => $coupon->getTimesUsed(),
261+
'is_primary' => $this->boolFlagToString((int)$coupon->getIsPrimary()),
262+
'created_at' => $coupon->getCreatedAt(),
263+
'type' => $this->getCouponCreationTypeAsString($coupon),
264+
'extension_attributes' => $coupon->getExtensionAttributes()
265+
];
266+
},
267+
$coupons
268+
);
269+
return json_encode($coupons);
270+
}
271+
219272
/**
220273
* Get header fields
221274
*
@@ -248,6 +301,7 @@ public function getHeaderFields(): array
248301
self::ATTR_USE_AUTO_GENERATION,
249302
self::ATTR_USES_PER_COUPON,
250303
self::ATTR_PRIMARY_COUPON,
304+
self::ATTR_COUPONS,
251305
self::ATTR_SIMPLE_FREE_SHIPPING,
252306
self::ATTR_STORE_LABELS,
253307
self::ATTR_WEBSITE_IDS,

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)