Skip to content

Commit 8c33e1f

Browse files
danielmetaconnollyDaniel M Connolly
andauthored
Syncing shipping profiles for each store in Magento (#404)
* changes * change * changes * changes --------- Co-authored-by: Daniel M Connolly <[email protected]>
1 parent 06128d3 commit 8c33e1f

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

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

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use Magento\OfflineShipping\Model\Config\Backend\Tablerate;
2525
use Magento\Framework\Exception\FileSystemException;
26+
use Magento\Store\Model\StoreManagerInterface;
2627
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2728
use Magento\Framework\Filesystem;
2829
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
@@ -57,6 +58,11 @@ class ShippingSettingsUpdatePlugin
5758
*/
5859
protected $systemConfig;
5960

61+
/**
62+
* @var StoreManagerInterface
63+
*/
64+
private StoreManagerInterface $storeManager;
65+
6066
/**
6167
* Constructor for Shipping settings update plugin
6268
*
@@ -65,19 +71,22 @@ class ShippingSettingsUpdatePlugin
6571
* @param GraphAPIAdapter $graphApiAdapter
6672
* @param FBEHelper $fbeHelper
6773
* @param SystemConfig $systemConfig
74+
* @param StoreManagerInterface $storeManager
6875
*/
6976
public function __construct(
70-
ShippingDataFactory $shippingRatesFactory,
71-
FileSystem $fileSystem,
72-
GraphAPIAdapter $graphApiAdapter,
73-
FBEHelper $fbeHelper,
74-
SystemConfig $systemConfig
77+
ShippingDataFactory $shippingRatesFactory,
78+
FileSystem $fileSystem,
79+
GraphAPIAdapter $graphApiAdapter,
80+
FBEHelper $fbeHelper,
81+
SystemConfig $systemConfig,
82+
StoreManagerInterface $storeManager,
7583
) {
7684
$this->shippingRatesFactory = $shippingRatesFactory;
7785
$this->fileSystem = $fileSystem;
7886
$this->graphApiAdapter = $graphApiAdapter;
7987
$this->fbeHelper = $fbeHelper;
8088
$this->systemConfig = $systemConfig;
89+
$this->storeManager = $storeManager;
8190
}
8291

8392
/**
@@ -87,23 +96,27 @@ public function __construct(
8796
*/
8897
public function afterSave(): void
8998
{
90-
try {
91-
$shippingRates = $this->shippingRatesFactory->create();
92-
$fileBuilder = new ShippingFileBuilder($this->fileSystem);
93-
$shippingProfiles = [
94-
$shippingRates->buildTableRatesProfile(),
95-
$shippingRates->buildFlatRateProfile(),
96-
$shippingRates->buildFreeShippingProfile()
97-
];
98-
$file_uri = $fileBuilder->createFile($shippingProfiles);
99-
$partnerIntegrationId = $this->systemConfig->getCommercePartnerIntegrationId();
100-
$this->graphApiAdapter->uploadFile($partnerIntegrationId, $file_uri, "SHIPPING_PROFILES", "CREATE");
101-
} catch (Exception $e) {
102-
$this->fbeHelper->logExceptionImmediatelyToMeta($e, [
103-
'store_id' => $this->fbeHelper->getStore()->getId(),
104-
'event' => 'shipping_profile_sync',
105-
'event_type' => 'after_save'
106-
]);
99+
foreach ($this->storeManager->getStores() as $store) {
100+
try {
101+
$shippingRates = $this->shippingRatesFactory->create();
102+
$fileBuilder = new ShippingFileBuilder($this->fileSystem);
103+
$shippingProfiles = [
104+
$shippingRates->buildTableRatesProfile(),
105+
$shippingRates->buildFlatRateProfile(),
106+
$shippingRates->buildFreeShippingProfile()
107+
];
108+
$file_uri = $fileBuilder->createFile($shippingProfiles);
109+
$partnerIntegrationId = $this->systemConfig->getCommercePartnerIntegrationId($store->getId());
110+
$this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($store->getId()))
111+
->setAccessToken($this->systemConfig->getAccessToken($store->getId()));
112+
$this->graphApiAdapter->uploadFile($partnerIntegrationId, $file_uri, "SHIPPING_PROFILES", "CREATE");
113+
} catch (Exception $e) {
114+
$this->fbeHelper->logExceptionImmediatelyToMeta($e, [
115+
'store_id' => $this->fbeHelper->getStore()->getId(),
116+
'event' => 'shipping_profile_sync',
117+
'event_type' => 'after_save'
118+
]);
119+
}
107120
}
108121
}
109122
}

0 commit comments

Comments
 (0)