|
22 | 22 |
|
23 | 23 | use Exception;
|
24 | 24 | use Magento\Framework\Filesystem;
|
| 25 | +use Magento\Store\Api\Data\StoreInterface; |
25 | 26 | use Magento\Store\Model\StoreManagerInterface;
|
26 | 27 | use Meta\BusinessExtension\Helper\GraphAPIAdapter;
|
27 | 28 | use Meta\BusinessExtension\Helper\FBEHelper;
|
@@ -96,39 +97,59 @@ public function __construct(
|
96 | 97 | /**
|
97 | 98 | * Syncing shipping profiles to Meta
|
98 | 99 | *
|
99 |
| - * @param string|null $accessToken |
100 |
| - * @param string|null $partnerIntegrationId |
| 100 | + * @param string $event_type |
| 101 | + * @param StoreInterface|null $store |
101 | 102 | * @return void
|
102 | 103 | */
|
103 |
| - public function syncShippingProfiles(string $accessToken = null, string $partnerIntegrationId = null) |
| 104 | + public function syncShippingProfiles(string $event_type, StoreInterface $store = null) |
104 | 105 | {
|
| 106 | + if ($store !== null) { |
| 107 | + $this->syncShippingProfilesForStore($event_type, $store); |
| 108 | + return; |
| 109 | + } |
105 | 110 | foreach ($this->storeManager->getStores() as $store) {
|
106 |
| - try { |
107 |
| - $this->shippingData->setStoreId((int)$store->getId()); |
108 |
| - $shippingProfiles = [ |
109 |
| - $this->shippingData->buildShippingProfile(ShippingProfileTypes::TABLE_RATE), |
110 |
| - $this->shippingData->buildShippingProfile(ShippingProfileTypes::FLAT_RATE), |
111 |
| - $this->shippingData->buildShippingProfile(ShippingProfileTypes::FREE_SHIPPING), |
112 |
| - ]; |
113 |
| - $fileUri = $this->shippingFileBuilder->createFile($shippingProfiles); |
114 |
| - $accessToken = $accessToken ?? $this->systemConfig->getAccessToken($store->getId()); |
115 |
| - $partnerIntegrationId = $partnerIntegrationId ?? |
116 |
| - $this->systemConfig->getCommercePartnerIntegrationId($store->getId()); |
117 |
| - $this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($store->getId())) |
118 |
| - ->setAccessToken($accessToken); |
119 |
| - $this->graphApiAdapter->uploadFile( |
120 |
| - $partnerIntegrationId, |
121 |
| - $fileUri, |
122 |
| - 'SHIPPING_PROFILES', |
123 |
| - 'CREATE' |
124 |
| - ); |
125 |
| - } catch (Exception $e) { |
126 |
| - $this->fbeHelper->logExceptionImmediatelyToMeta($e, [ |
127 |
| - 'store_id' => $this->fbeHelper->getStore()->getId(), |
128 |
| - 'event' => 'shipping_profile_sync', |
129 |
| - 'event_type' => 'after_save' |
130 |
| - ]); |
| 111 | + $this->syncShippingProfilesForStore($event_type, $store); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Syncing shipping profiles for an individual store |
| 117 | + * |
| 118 | + * @param string $event_type |
| 119 | + * @param StoreInterface $store |
| 120 | + * @return void |
| 121 | + */ |
| 122 | + private function syncShippingProfilesForStore(string $event_type, StoreInterface $store) |
| 123 | + { |
| 124 | + try { |
| 125 | + $accessToken = $accessToken ?? $this->systemConfig->getAccessToken($store->getId()); |
| 126 | + if ($accessToken === null) { |
| 127 | + return; |
131 | 128 | }
|
| 129 | + $this->shippingData->setStoreId((int)$store->getId()); |
| 130 | + $shippingProfiles = [ |
| 131 | + $this->shippingData->buildShippingProfile(ShippingProfileTypes::TABLE_RATE), |
| 132 | + $this->shippingData->buildShippingProfile(ShippingProfileTypes::FLAT_RATE), |
| 133 | + $this->shippingData->buildShippingProfile(ShippingProfileTypes::FREE_SHIPPING), |
| 134 | + ]; |
| 135 | + $fileUri = $this->shippingFileBuilder->createFile($shippingProfiles); |
| 136 | + $accessToken = $accessToken ?? $this->systemConfig->getAccessToken($store->getId()); |
| 137 | + $partnerIntegrationId = $partnerIntegrationId ?? |
| 138 | + $this->systemConfig->getCommercePartnerIntegrationId($store->getId()); |
| 139 | + $this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($store->getId())) |
| 140 | + ->setAccessToken($accessToken); |
| 141 | + $this->graphApiAdapter->uploadFile( |
| 142 | + $partnerIntegrationId, |
| 143 | + $fileUri, |
| 144 | + 'SHIPPING_PROFILES', |
| 145 | + 'CREATE' |
| 146 | + ); |
| 147 | + } catch (Exception $e) { |
| 148 | + $this->fbeHelper->logExceptionImmediatelyToMeta($e, [ |
| 149 | + 'store_id' => $this->fbeHelper->getStore()->getId(), |
| 150 | + 'event' => 'shipping_profile_sync', |
| 151 | + 'event_type' => $event_type |
| 152 | + ]); |
132 | 153 | }
|
133 | 154 | }
|
134 | 155 | }
|
0 commit comments