20 private $exportedProductRepository;
27 $this->processor = $processor;
28 $this->exportedProductRepository = $facebookProductRepository;
33 \Bitrix\Catalog\v2\Integration\Iblock\BrandProperty::createFacebookBrandProperty();
43 $collection = $this->exportedProductRepository->loadCollection($ids);
44 $exportedProductsIds = $collection->getProductIdList();
46 if (!empty($exportedProductsIds))
54 private function getProductEntitiesByIds(array $ids): array
64 $sku = ServiceContainer::getRepositoryFacade()->loadVariation($id);
79 if (!$result->isSuccess())
84 $skus = $this->getProductEntitiesByIds($ids);
87 if (!$result->isSuccess())
92 $preparedSku = $this->prepareProducts($skus);
96 ->batchCatalogProducts($preparedSku);
99 if ($response->isSuccess())
101 $responseData = $response->getData();
102 $queueId = $responseData[
'queue_ids'][0] ??
null;
103 $validationStatus = $responseData[
'validation_status'] ??
null;
104 $productKeys = array_keys($preparedSku);
106 if ($validationStatus)
108 $errorProducts = $this->parseValidationStatus($validationStatus);
109 $result->setData([
'ERROR_PRODUCTS' => $errorProducts]);
110 $errorProductKeys = array_keys($errorProducts);
111 $productKeys = array_diff($productKeys, $errorProductKeys);
112 $this->exportedProductRepository->save($errorProducts);
117 \Bitrix\Catalog\v2\Integration\Seo\Entity\ExportedProductQueueTable::add([
118 'QUEUE_ID' => $queueId,
119 'PRODUCT_IDS' => \
Bitrix\Main\Web\Json::encode($productKeys),
121 $result->setData([
'QUEUE_ID' => $queueId]);
126 $result->addErrors($response->getErrors());
136 foreach ($skus as $sku)
138 $res = $this->processor->validate($sku);
139 if (!$res->isSuccess())
141 $result->addErrors($res->getErrors());
148 private function prepareProducts($skus): array
150 $preparedProducts = [];
152 foreach ($skus as $sku)
154 $result = $this->processor->prepare($sku);
155 if ($result->isSuccess())
157 foreach ($result->getData() as $key => $value)
159 $preparedProducts[$key] = $value;
164 return $preparedProducts;
167 private function getCatalog(): Catalog
169 static $catalog =
null;
171 if ($catalog ===
null)
174 $catalog = $service->getCatalog($service::TYPE_FACEBOOK);
188 $region = \Bitrix\Main\Application::getInstance()->getLicense()->getRegion();
189 if ($region ===
null || $region ===
'ru')
194 if (Option::get(
'catalog',
'fb_product_export_enabled',
'N') !==
'Y')
199 if (!Loader::includeModule(
'seo'))
214 return $this->getAuth();
219 return $this->getAuth()->isSuccess();
226 return $this->getCatalog()->getPageId();
236 return $this->getCatalog()->getCatalogId();
242 private function getAuth():
Result
246 if (!Loader::includeModule(
'seo'))
248 return $result->addError(
new Error(
'The SEO module is not installed.'));
253 if (!$service::getAuthAdapter($service::TYPE_FACEBOOK)->
hasAuth())
255 return $result->addError(
new Error(
'Facebook account with business suite is not authorized.'));
258 if (!$this->getCatalog())
260 return $result->addError(
new Error(
'Facebook account is not authorized to use catalog features.'));
266 private function parseValidationStatus(array $validationStatus): array
268 $parsedProductErrors = [];
269 foreach ($validationStatus as $validationStatusElement)
271 $id = $this->processor->getProductIdByRetailerId($validationStatusElement[
'retailer_id']);
272 $error = $validationStatusElement[
'errors'][0][
'message'] ??
null;
273 $parsedProductErrors[$id] = [
279 return $parsedProductErrors;
284 return $this->exportedProductRepository->getProductsByIds($productIds);
287 private function getFacebookProductIds(array $productIds): array
289 $facebookProductIds = [];
291 $preparedProductIds = [];
292 foreach ($productIds as $productId)
294 $preparedProductIds[] = $this->processor->getEntityRetailerId($productId);
300 ->getProductsInfo($preparedProductIds)
303 if (!$response->isSuccess())
305 return $facebookProductIds;
308 $productsInfo = $response->getData();
309 foreach ($productsInfo as $productInfo)
311 $facebookProductIds[] = $productInfo[
'id'];
314 return $facebookProductIds;
317 private function processWebhook(
int $queueId, array $errors): void
319 $queueData = ExportedProductQueueTable::getByPrimary($queueId)->fetch();
324 ExportedProductQueueTable::delete($queueId);
326 $preparedErrors = [];
327 foreach ($errors as $error)
329 $preparedErrors[$this->processor->getProductIdByRetailerId($error[
'id'])] = $error[
'message'];
331 $productIds = \Bitrix\Main\Web\Json::decode($queueData[
'PRODUCT_IDS']);
332 $preparedProducts = [];
333 foreach ($productIds as $productId)
335 $preparedProducts[$productId] = [
337 'ERROR' => $preparedErrors[$productId],
341 $this->exportedProductRepository->save($preparedProducts);
343 $facebookProductIds = [];
344 if (empty($preparedErrors))
346 $facebookProductIds = $this->getFacebookProductIds($productIds);
349 $event = new \Bitrix\Main\Event(
351 'onFacebookCompilationExportFinished',
353 'QUEUE_ID' => $queueId,
354 'ERROR_PRODUCTS' => $preparedErrors,
355 'FACEBOOK_PRODUCT_IDS' => $facebookProductIds,
363 if (!Loader::includeModule(
'crm'))
367 $crmCatalogIblockId = \CCrmCatalog::EnsureDefaultExists() ?: 0;
368 $instance = ServiceContainer::get(
'integration.seo.facebook.facade', [
369 'iblockId' => $crmCatalogIblockId,
371 $queueId = $event->getParameter(
'payload')[
'id'];
372 $errors = Json::decode($event->getParameter(
'payload')[
'errors']);
373 $instance->processWebhook($queueId, $errors);