From da5d2b6a31fbf18983da0c55e8cc9bf10cdaf09d Mon Sep 17 00:00:00 2001 From: LiorGingi Date: Mon, 25 Apr 2022 11:47:17 +0300 Subject: [PATCH 1/2] enhance(Sync/Catalog/Data):added getting image URL from Magento/Catalog/Helper/Image --- Model/Sync/Catalog/Data.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Model/Sync/Catalog/Data.php b/Model/Sync/Catalog/Data.php index 0391a5e9..ee06e6a6 100644 --- a/Model/Sync/Catalog/Data.php +++ b/Model/Sync/Catalog/Data.php @@ -2,6 +2,7 @@ namespace Yotpo\Core\Model\Sync\Catalog; +use Magento\Catalog\Helper\Image as CatalogImageHelper; use Magento\CatalogInventory\Model\StockRegistry; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; @@ -20,6 +21,11 @@ */ class Data extends Main { + /** + * @var CatalogImageHelper + */ + private $catalogImageHelper; + /** * @var YotpoCoreConfig */ @@ -167,6 +173,7 @@ class Data extends Main /** * Data constructor. + * @param CatalogImageHelper $catalogImageHelper * @param YotpoCoreConfig $yotpoCoreConfig * @param YotpoResource $yotpoResource * @param Configurable $resourceConfigurable @@ -177,6 +184,7 @@ class Data extends Main * @param YotpoCoreCatalogLogger $yotpoCatalogLogger */ public function __construct( + CatalogImageHelper $catalogImageHelper, YotpoCoreConfig $yotpoCoreConfig, YotpoResource $yotpoResource, Configurable $resourceConfigurable, @@ -186,6 +194,7 @@ public function __construct( StockRegistry $stockRegistry, YotpoCoreCatalogLogger $yotpoCatalogLogger ) { + $this->catalogImageHelper = $catalogImageHelper; $this->yotpoCoreConfig = $yotpoCoreConfig; $this->yotpoResource = $yotpoResource; $this->resourceConfigurable = $resourceConfigurable; @@ -389,8 +398,7 @@ public function attributeMapping(Product $item) } if (isset($attr['type']) && $attr['type'] === 'image') { - $baseUrl = $this->yotpoCoreConfig->getBaseUrl(UrlInterface::URL_TYPE_MEDIA); - $data = $data ? $baseUrl . 'catalog/product' . $data : ""; + $data = $this->getProductImageUrl($item); } $value = $data; } elseif (isset($attr['method']) && $attr['method']) { @@ -618,4 +626,16 @@ private function filterNotConfigurableProducts($productIds) return $filteredProductIds; } + + /** + * Get Product Image from product object + * + * @param Product $product + * @param string $imageId + * @return string|null + */ + private function getProductImageUrl($product, $imageId = 'product_page_image_large') + { + return $this->catalogImageHelper->init($product, $imageId)->getUrl(); + } } From 1203c019cb8260f40133e8d0d6a99bf0acdeeb54 Mon Sep 17 00:00:00 2001 From: LiorGingi Date: Mon, 25 Apr 2022 12:00:14 +0300 Subject: [PATCH 2/2] style(Sync/Catalog/Data#attributeMapping):changed else-if to switch-case --- Model/Sync/Catalog/Data.php | 93 +++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/Model/Sync/Catalog/Data.php b/Model/Sync/Catalog/Data.php index ee06e6a6..2253fdf9 100644 --- a/Model/Sync/Catalog/Data.php +++ b/Model/Sync/Catalog/Data.php @@ -59,7 +59,7 @@ class Data extends Main /** * @var array */ - protected $mappingAttributes = [ + protected $attributesMapping = [ 'row_id' => [ 'default' => 1, 'attr_code' => 'row_id' @@ -201,7 +201,7 @@ public function __construct( $this->productRepository = $productRepository; $this->collectionFactory = $collectionFactory; $this->stockRegistry = $stockRegistry; - $this->mappingAttributes['row_id']['attr_code'] = $this->yotpoCoreConfig->getEavRowIdFieldName(); + $this->attributesMapping['row_id']['attr_code'] = $this->yotpoCoreConfig->getEavRowIdFieldName(); $this->logger = $yotpoCatalogLogger; parent::__construct($resourceConnection); } @@ -378,64 +378,69 @@ protected function prepareOptions($syncItems, $configIds, $productObjects) */ public function attributeMapping(Product $item) { - $itemArray = []; - $mapAttributes = $this->mappingAttributes; + $itemAttributesData = []; - foreach ($mapAttributes as $key => $attr) { + foreach ($this->attributesMapping as $attributeKey => $attributeDetails) { try { - if ($key === 'gtins') { - $value = $this->prepareGtinsData($attr, $item); - } elseif ($key === 'custom_properties') { - $value = $this->prepareCustomProperties($attr, $item); - } elseif ($key === 'is_discontinued') { - $value = false; - } else { - if ($attr['default']) { - $data = $item->getData($attr['attr_code']); - - if (isset($attr['type']) && $attr['type'] === 'url') { - $data = $item->getProductUrl(); + switch ($attributeKey) { + case 'gtins': + $value = $this->prepareGtinsData($attributeDetails, $item); + break; + case 'custom_properties': + $value = $this->prepareCustomProperties($attributeDetails, $item); + break; + case 'is_discontinued': + $value = false; + break; + case 'url': + $value = $item->getProductUrl(); + break; + case 'image_url': + $value = $this->getProductImageUrl($item); + break; + default: + if (!$attributeDetails['default'] && isset($attributeDetails['method']) && $attributeDetails['method']) { + + $configKey = isset($attributeDetails['attr_code']) && $attributeDetails['attr_code'] ? + $attributeDetails['attr_code'] : ''; + + $method = $attributeDetails['method']; + $itemValue = $this->$method($item, $configKey); + if ($itemValue) { + $value = $itemValue; + } elseif ($method == 'getProductPrice') { + $value = 0.00; + } else { + $value = $itemValue; + } + } else { + $value = ''; } - - if (isset($attr['type']) && $attr['type'] === 'image') { - $data = $this->getProductImageUrl($item); + if ($attributeKey == 'group_name' && $value) { + $value = substr((string)$value, 0, 100); + $value = strtolower($value); + $value = str_replace(' ', '_', $value); + $value = preg_replace('/[^A-Za-z0-9_-]/', '-', $value); } - $value = $data; - } elseif (isset($attr['method']) && $attr['method']) { - - $configKey = isset($attr['attr_code']) && $attr['attr_code'] ? - $attr['attr_code'] : ''; - - $method = $attr['method']; - $itemValue = $this->$method($item, $configKey); - $value = $itemValue ?: ($method == 'getProductPrice' ? 0.00 : $itemValue); - } else { - $value = ''; - } - if ($key == 'group_name' && $value) { - $value = strtolower($value); - $value = str_replace(' ', '_', $value); - $value = preg_replace('/[^A-Za-z0-9_-]/', '-', $value); - $value = substr((string)$value, 0, 100); - } } - $itemArray[$key] = $value; - if (($key == 'custom_properties' || $key == 'gtins') && !$value) { - unset($itemArray[$key]); + + $itemAttributesData[$attributeKey] = $value; + if (($attributeKey == 'custom_properties' || $attributeKey == 'gtins') && !$value) { + unset($itemAttributesData[$attributeKey]); } } catch (\Exception $e) { $this->logger->info( __( 'Exception raised within attributeMapping - $key: %1, $attr: %2 Exception Message: %3', - $key, - $attr, + $attributeKey, + $attributeDetails, $e->getMessage() ) ); } } - return $itemArray; + return $itemAttributesData; } /**