Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Model/Sync/Catalog/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ public function getSyncItems($items, $isVariantsDataIncluded)
$productIdsToParentIdsMap = [];
$failedVariantsIds = [];
if (!$isVariantsDataIncluded) {
$productIdsToConfigurableIdsMapToCheck = $this->yotpoResource->getConfigProductIds($productsId, $failedVariantsIds);
$productIdsToConfigurableIdsMapToCheck = $this->yotpoResource->getConfigProductIds(
$productsId,
$failedVariantsIds
);
foreach ($failedVariantsIds as $failedVariantId) {
unset($productsId[array_search($failedVariantId, $productsId)]);
unset($productsObject[$failedVariantId]);
Expand Down
126 changes: 85 additions & 41 deletions Model/Sync/Catalog/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ public function syncItems($collectionItems, $storeId, $isVisibleVariantsSync = f
$updatedParentYotpoId = $this->forceParentProductSyncToYotpo(
$storeId,
$itemEntityId,

$parentItemId,
$yotpoSyncTableItemsData,
$parentItemsIds,
Expand All @@ -306,13 +305,18 @@ public function syncItems($collectionItems, $storeId, $isVisibleVariantsSync = f
if (!$updatedParentYotpoId) {
continue;
}
} elseif ($this->isProductParentYotpoIdChanged($itemEntityId, $parentItemId, $yotpoSyncTableItemsData)) {
} elseif ($this->isProductParentYotpoIdChanged(
$itemEntityId,
$parentItemId,
$yotpoSyncTableItemsData
)) {
$parentProductYotpoId = $yotpoSyncTableItemsData[$parentItemId]['yotpo_id'];
$yotpoSyncTableItemsData[$itemEntityId]['yotpo_id_parent'] = $parentProductYotpoId;

$this->yotpoCatalogLogger->info(
__(
'Yotpo ID of parent product changed - Store ID: %1, Store Name: %2, Parent Entity ID: %3, New Yotpo ID: %4',
'Yotpo ID of parent product changed -
Store ID: %1, Store Name: %2, Parent Entity ID: %3, New Yotpo ID: %4',
$storeId,
$this->coreConfig->getStoreName($storeId),
$parentItemId,
Expand Down Expand Up @@ -356,7 +360,10 @@ public function syncItems($collectionItems, $storeId, $isVisibleVariantsSync = f
$hasFailedCreatingAnyProduct = true;
}

if ($this->isVariantUpsertSyncMethod($apiRequestParams['method']) && $responseCode == CoreConfig::NOT_FOUND_RESPONSE_CODE) {
if ($this->isVariantUpsertSyncMethod(
$apiRequestParams['method']
) && $responseCode == CoreConfig::NOT_FOUND_RESPONSE_CODE
) {
$updatedParentYotpoId = $this->forceParentProductSyncToYotpo(
$storeId,
$itemEntityId,
Expand Down Expand Up @@ -414,7 +421,12 @@ public function syncItems($collectionItems, $storeId, $isVisibleVariantsSync = f

if (count($returnResponse['failed_product_ids_for_retry'])) {
foreach ($returnResponse['failed_product_ids_for_retry'] as $retryId) {
if ($this->isImmediateRetry($response, $this->entity, $isVisibleVariantsSync.$retryId, $storeId)) {
if ($this->isImmediateRetry(
$response,
$this->entity,
$isVisibleVariantsSync.$retryId,
$storeId
)) {
$this->retryItems[$storeId][$retryId] = $retryId;
$this->setImmediateRetryAlreadyDone(
$this->entity,
Expand Down Expand Up @@ -447,8 +459,8 @@ public function syncItems($collectionItems, $storeId, $isVisibleVariantsSync = f
$apiRequestParams['method'],
$response->getData('is_success'),
$currentItemYotpoId,
$apiRequestParams['yotpo_id'])
) {
$apiRequestParams['yotpo_id']
)) {
$this->resetVariantsSyncWithUpdatedParentYotpoId(
$storeId,
$itemEntityId,
Expand Down Expand Up @@ -559,10 +571,10 @@ protected function processDeleteData()
}
$params = $this->getDeleteApiParams($itemData, 'yotpo_id');
$itemDataRequest = ['is_discontinued' => true];
$responseObject = $this->handleRequest($itemId, $itemDataRequest, $params);
$response = $responseObject['response'];
$params['method'] = $responseObject['method'];
$params['yotpo_id'] = $responseObject['yotpo_id'];
$responseObject = $this->handleRequest($itemId, $itemDataRequest, $params);
$response = $responseObject['response'];
$params['method'] = $responseObject['method'];
$params['yotpo_id'] = $responseObject['yotpo_id'];
$returnResponse = $this->processResponse($params, $response, $tempDeleteQry, $itemData);
if ($this->isImmediateRetryResponse($response->getData('status'))) {
$response = $this->processDeleteRetry($params, $apiParam, $itemData, $itemId);
Expand All @@ -573,7 +585,8 @@ protected function processDeleteData()
$this->updateProductSyncAttribute($storeId, $itemId);
$this->yotpoCatalogLogger->info(
__(
'Exception raised within processDeleteData - $itemId: %1, $itemData: %2 Exception Message: %3',
'Exception raised within processDeleteData -
$itemId: %1, $itemData: %2 Exception Message: %3',
$itemId,
$itemData,
$e->getMessage()
Expand Down Expand Up @@ -609,10 +622,10 @@ protected function processUnAssignData()
}
$params = $this->getDeleteApiParams($itemData, 'yotpo_id_unassign');
$itemDataRequest = ['is_discontinued' => true];
$responseObject = $this->handleRequest($itemId, $itemDataRequest, $params);
$response = $responseObject['response'];
$params['method'] = $responseObject['method'];
$params['yotpo_id'] = $responseObject['yotpo_id'];
$responseObject = $this->handleRequest($itemId, $itemDataRequest, $params);
$response = $responseObject['response'];
$params['method'] = $responseObject['method'];
$params['yotpo_id'] = $responseObject['yotpo_id'];
$returnResponse = $this->processResponse($params, $response, $tempDeleteQry, $itemData);
if ($this->isImmediateRetryResponse($response->getData('status'))) {
$response = $this->processDeleteRetry($params, $apiParam, $itemData, $itemId);
Expand All @@ -623,7 +636,8 @@ protected function processUnAssignData()
$this->updateProductSyncAttribute($storeId, $itemId);
$this->yotpoCatalogLogger->info(
__(
'Exception raised within processUnAssignData - $itemId: %1, $itemData: %2 Exception Message: %3',
'Exception raised within processUnAssignData -
$itemId: %1, $itemData: %2 Exception Message: %3',
$itemId,
$itemData,
$e->getMessage()
Expand Down Expand Up @@ -790,7 +804,10 @@ public function getUnSyncedProductIds($productIds, $visibleItems, $storeId)

if (!$this->coreConfig->isCatalogSyncActive($storeId)) {
$productsIdsToCheck = array_diff($productIds, $productsIdsToCreate);
$productsIdsToUpdate = $this->productsSyncService->findProductsThatShouldBeSyncedByAttribute($productsIdsToCheck);
$productsIdsToUpdate =
$this->productsSyncService->findProductsThatShouldBeSyncedByAttribute(
$productsIdsToCheck
);
}

return array_merge($productsIdsToUpdate, $productsIdsToCreate);
Expand Down Expand Up @@ -861,8 +878,8 @@ private function prepareSyncTableDataToUpdate(
}

/**
* @param $storeId
* @param $itemRowId
* @param int $storeId
* @param int $itemRowId
* @return void
*/
private function updateProductSyncAttribute($storeId, $itemRowId)
Expand All @@ -881,13 +898,20 @@ private function updateProductSyncAttribute($storeId, $itemRowId)
* @param array <mixed> $yotpoSyncTableItemsData
* @param array <mixed> $parentItemsIds
* @param array <mixed> $yotpoFormatItemData
* @return integer
* @return integer|string|null
*/
private function forceParentProductSyncToYotpo($storeId, $itemEntityId, $parentItemId, $yotpoSyncTableItemsData, $parentItemsIds, $yotpoFormatItemData)
{
private function forceParentProductSyncToYotpo(
$storeId,
$itemEntityId,
$parentItemId,
$yotpoSyncTableItemsData,
$parentItemsIds,
$yotpoFormatItemData
) {
$this->yotpoCatalogLogger->info(
__(
'Start syncing parent product that does not exist in Yotpo - Store ID: %1, Store Name: %2, Item Entity ID: %3, Parent Entity ID: %4',
'Start syncing parent product that does not exist in Yotpo -
Store ID: %1, Store Name: %2, Item Entity ID: %3, Parent Entity ID: %4',
$storeId,
$this->coreConfig->getStoreName($storeId),
$itemEntityId,
Expand All @@ -909,7 +933,8 @@ private function forceParentProductSyncToYotpo($storeId, $itemEntityId, $parentI
if ($parentProductYotpoId) {
$this->yotpoCatalogLogger->info(
__(
'Finished syncing parent product that does not exist in Yotpo - Store ID: %1, Store Name: %2, Parent Entity ID: %3, Yotpo ID: %4',
'Finished syncing parent product that does not exist in Yotpo -
Store ID: %1, Store Name: %2, Parent Entity ID: %3, Yotpo ID: %4',
$storeId,
$this->coreConfig->getStoreName($storeId),
$parentItemId,
Expand All @@ -924,7 +949,8 @@ private function forceParentProductSyncToYotpo($storeId, $itemEntityId, $parentI
} else {
$this->yotpoCatalogLogger->info(
__(
'Failed creating parent product for a variant - Store ID: %1, Store Name: %2, Parent Entity ID: %3, Yotpo ID: %4',
'Failed creating parent product for a variant -
Store ID: %1, Store Name: %2, Parent Entity ID: %3, Yotpo ID: %4',
$storeId,
$this->coreConfig->getStoreName($storeId),
$parentItemId,
Expand All @@ -941,7 +967,7 @@ private function forceParentProductSyncToYotpo($storeId, $itemEntityId, $parentI
* @param array <mixed> $yotpoSyncTableItemsData
* @param array <mixed> $parentItemsIds
* @param array <mixed> $yotpoFormatItemData
* @return string|null
* @return string|int|null
*/
private function ensureEntityExistenceAsProductInYotpo(
$parentId,
Expand Down Expand Up @@ -973,7 +999,8 @@ private function ensureEntityExistenceAsProductInYotpo(
if ($response && !$response->getData(CoreConfig::YOTPO_SYNC_RESPONSE_IS_SUCCESS_KEY)) {
$this->yotpoCatalogLogger->info(
__(
'Failed syncing missing variant parent product to Yotpo - Parent Entity ID: %1, Status Code: %2, Reason: %3',
'Failed syncing missing variant parent product to Yotpo -
Parent Entity ID: %1, Status Code: %2, Reason: %3',
$parentId,
$response->getData('status'),
$response->getData('reason')
Expand Down Expand Up @@ -1028,9 +1055,9 @@ private function shouldForceProductCollectionsResync($response, $apiRequestParam
* @param bool $isVisibleVariantsSync
* @param string $syncMethod
* @param bool $isRequestSuccessful
* @param integer $currentItemYotpoId
* @param integer|string $currentItemYotpoId
* @param integer $returnedItemYotpoId
* @return
* @return boolean
*/
private function shouldForceVariantsSyncFollowingProductUpsert(
$isVisibleVariantsSync,
Expand Down Expand Up @@ -1064,8 +1091,8 @@ private function isVariantUpsertSyncMethod($method)
}

/**
* @param integer $currentItemYotpoId
* @param integer $returnedItemYotpoId
* @param integer|string $currentItemYotpoId
* @param integer|string $returnedItemYotpoId
* @return boolean
*/
private function isItemYotpoIdChanged($currentItemYotpoId, $returnedItemYotpoId)
Expand All @@ -1075,8 +1102,10 @@ private function isItemYotpoIdChanged($currentItemYotpoId, $returnedItemYotpoId)

/**
* @param integer $storeId
* @param integer $currentParentYotpoId
* @param integer $updatedParentYotpoId
* @param integer $parentEntityId
* @param integer|string $currentParentYotpoId
* @param integer|string $updatedParentYotpoId
* @param integer $syncedToYotpoProductAttributeId
* @return void
*/
private function resetVariantsSyncWithUpdatedParentYotpoId(
Expand All @@ -1088,31 +1117,46 @@ private function resetVariantsSyncWithUpdatedParentYotpoId(
) {
$this->yotpoCatalogLogger->info(
__(
'Yotpo Parent Product ID changed, forcing variants re-sync. Store ID: %1, Entity ID: %2, Current Yotpo ID: %3, Updated Yotpo ID: %4',
'Yotpo Parent Product ID changed, forcing variants re-sync.
Store ID: %1, Entity ID: %2, Current Yotpo ID: %3, Updated Yotpo ID: %4',
$storeId,
$parentEntityId,
$currentParentYotpoId,
$updatedParentYotpoId
)
);

$variantIds = $this->productsSyncService->getProductIdsFromSyncTableByStoreIdAndParentYotpoId($storeId, $currentParentYotpoId);
$variantIds = $this->productsSyncService->getProductIdsFromSyncTableByStoreIdAndParentYotpoId(
$storeId,
$currentParentYotpoId
);
if (!$variantIds) {
return;
}

$this->productsSyncService->updateYotpoIdParentInSyncTableByStoreIdAndVariantIds($storeId, $variantIds, $updatedParentYotpoId);
$this->updateProductSyncAttributeByStoreIdAndProductIds($storeId, $variantIds, $syncedToYotpoProductAttributeId);
$this->productsSyncService->updateYotpoIdParentInSyncTableByStoreIdAndVariantIds(
$storeId,
$variantIds,
$updatedParentYotpoId
);
$this->updateProductSyncAttributeByStoreIdAndProductIds(
$storeId,
$variantIds,
$syncedToYotpoProductAttributeId
);
}

/**
* @param integer $storeId
* @param array $productIds
* @param array<mixed> $productIds
* @param integer $syncedToYotpoProductAttributeId
* @return void
*/
private function updateProductSyncAttributeByStoreIdAndProductIds($storeId, $productIds, $syncedToYotpoProductAttributeId)
{
private function updateProductSyncAttributeByStoreIdAndProductIds(
$storeId,
$productIds,
$syncedToYotpoProductAttributeId
) {
$connection = $this->resourceConnection->getConnection();
$condition = [
'store_id = ?' => $storeId,
Expand Down
5 changes: 4 additions & 1 deletion Model/Sync/Catalog/Processor/CatalogRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public function handleProductUpsert($itemEntityId, $yotpoItemData, $yotpoProduct
if ($responseStatusCode == CoreConfig::BAD_REQUEST_RESPONSE_CODE && !$yotpoProductId) {
$minimalProductRequest = $this->catalogData->getMinimalProductRequestData($yotpoItemData);
$responseObject = $this->upsertProduct($yotpoProductId, $minimalProductRequest);
} elseif (in_array($responseStatusCode, [CoreConfig::NOT_FOUND_RESPONSE_CODE, CoreConfig::CONFLICT_RESPONSE_CODE])) {
} elseif (in_array(
$responseStatusCode,
[CoreConfig::NOT_FOUND_RESPONSE_CODE, CoreConfig::CONFLICT_RESPONSE_CODE]
)) {
try {
$yotpoProductId = $this->getYotpoItemIdFromItemEntityId($itemEntityId, 'products');
} catch (UnexpectedValueException $e) {
Expand Down
3 changes: 2 additions & 1 deletion Model/Sync/Catalog/Processor/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ public function isProductParentYotpoIdChanged($itemEntityId, $parentItemId, $yot
{
return array_key_exists($itemEntityId, $yotpoSyncTableItemsData)
&& array_key_exists($parentItemId, $yotpoSyncTableItemsData)
&& $yotpoSyncTableItemsData[$itemEntityId]['yotpo_id_parent'] != $yotpoSyncTableItemsData[$parentItemId]['yotpo_id'];
&& $yotpoSyncTableItemsData[$itemEntityId]['yotpo_id_parent'] !=
$yotpoSyncTableItemsData[$parentItemId]['yotpo_id'];
}

/**
Expand Down
17 changes: 10 additions & 7 deletions Model/Sync/Catalog/Services/ProductsSyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function updateSyncTable($syncDataRecord)

/**
* @param integer $storeId
* @param integer $parentYotpoId
* @return array
* @param integer|string $parentYotpoId
* @return array<mixed>
*/
public function getProductIdsFromSyncTableByStoreIdAndParentYotpoId($storeId, $parentYotpoId)
{
Expand All @@ -79,7 +79,7 @@ public function getProductIdsFromSyncTableByStoreIdAndParentYotpoId($storeId, $p

/**
* @param array<integer> $productIds
* @return array
* @return array<mixed>
*/
public function findProductsThatShouldBeSyncedByAttribute($productIds)
{
Expand All @@ -104,12 +104,15 @@ public function findProductsThatShouldBeSyncedByAttribute($productIds)

/**
* @param integer $storeId
* @param array $variantIds
* @param integer $yotpoIdParentToBeUpdated
* @param array<mixed> $variantIds
* @param integer|string $yotpoIdParentToBeUpdated
* @return void
*/
public function updateYotpoIdParentInSyncTableByStoreIdAndVariantIds($storeId, $variantIds, $yotpoIdParentToBeUpdated)
{
public function updateYotpoIdParentInSyncTableByStoreIdAndVariantIds(
$storeId,
$variantIds,
$yotpoIdParentToBeUpdated
) {
$connection = $this->resourceConnection->getConnection();
$condition = [
'store_id = ?' => $storeId,
Expand Down
2 changes: 1 addition & 1 deletion Model/Sync/Catalog/YotpoResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getUnAssignedCollection(int $storeId, int $limit): array
/**
* Get config parent IDs
* @param array<int, int> $simpleIds
* @param $failedSimpleIds
* @param array<mixed> $failedSimpleIds
* @return array<mixed>
*/
public function getConfigProductIds(array $simpleIds, &$failedSimpleIds): array
Expand Down
4 changes: 3 additions & 1 deletion Model/Sync/Category/Processor/ProcessByCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,9 @@ public function deleteCollections()
$categoryId
)
);
$categoryProductsIds = $this->collectionsProductsService->getCategoryProductsIdsFromSyncTable($categoryId);
$categoryProductsIds = $this->collectionsProductsService->getCategoryProductsIdsFromSyncTable(
$categoryId
);
if ($categoryProductsIds) {
$this->collectionsProductsService->assignCategoryProductsForCollectionsProductsSync(
$categoryProductsIds,
Expand Down
Loading