diff --git a/Model/Sync/Catalog/Data.php b/Model/Sync/Catalog/Data.php index 0391a5e9..2253fdf9 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 */ @@ -53,7 +59,7 @@ class Data extends Main /** * @var array */ - protected $mappingAttributes = [ + protected $attributesMapping = [ 'row_id' => [ 'default' => 1, 'attr_code' => 'row_id' @@ -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,13 +194,14 @@ public function __construct( StockRegistry $stockRegistry, YotpoCoreCatalogLogger $yotpoCatalogLogger ) { + $this->catalogImageHelper = $catalogImageHelper; $this->yotpoCoreConfig = $yotpoCoreConfig; $this->yotpoResource = $yotpoResource; $this->resourceConfigurable = $resourceConfigurable; $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); } @@ -369,65 +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') { - $baseUrl = $this->yotpoCoreConfig->getBaseUrl(UrlInterface::URL_TYPE_MEDIA); - $data = $data ? $baseUrl . 'catalog/product' . $data : ""; + 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; } /** @@ -618,4 +631,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(); + } }