From 48c9d46fddc5bd6737e648acc76cf9b81af306cb Mon Sep 17 00:00:00 2001 From: Agata Firlejczyk Date: Thu, 30 Apr 2020 18:24:48 +0200 Subject: [PATCH] Configure category attributes for export - fix excluding attributes from category. --- CHANGELOG.MD | 3 + .../Model/Attributes/CategoryAttributes.php | 91 +++++++++++++++++++ .../Attributes/CategoryChildAttributes.php | 16 +--- .../DataProvider/Category/AttributeData.php | 51 +++++++++-- .../DataProvider/Product/MediaGalleryData.php | 2 +- .../Model/SystemConfig/CategoryConfig.php | 18 +++- 6 files changed, 159 insertions(+), 22 deletions(-) create mode 100644 src/module-vsbridge-indexer-catalog/Model/Attributes/CategoryAttributes.php diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 484ba049..93eff4e0 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -2,6 +2,9 @@ ## Unreleased +## [1.16.1] (2020.04.14) +- Fix excluding attributes from category. ([#267](https://github.com/DivanteLtd/magento2-vsbridge-indexer/issues/267)) + ### [1.16.0] (2020.20.24) ### Added diff --git a/src/module-vsbridge-indexer-catalog/Model/Attributes/CategoryAttributes.php b/src/module-vsbridge-indexer-catalog/Model/Attributes/CategoryAttributes.php new file mode 100644 index 00000000..a0642121 --- /dev/null +++ b/src/module-vsbridge-indexer-catalog/Model/Attributes/CategoryAttributes.php @@ -0,0 +1,91 @@ +config = $categoryConfig; + } + + /** + * Retrieve required attributes for category + * + * @param int $storeId + * + * @return array + */ + public function getRequiredAttributes(int $storeId) + { + $attributes = $this->config->getAllowedAttributesToIndex($storeId); + + if (!empty($attributes)) { + $attributes = array_merge($attributes, self::MINIMAL_ATTRIBUTE_SET); + + return array_unique($attributes); + } + + return $attributes; + } + + /** + * @param int $storeId + * + * @return bool + */ + public function canAddAvailableSortBy(int $storeId): bool + { + return $this->isAttributeAllowed('available_sort_by', $storeId); + } + + /** + * @param int $storeId + * + * @return bool + */ + public function canAddDefaultSortBy(int $storeId): bool + { + return $this->isAttributeAllowed('default_sort_by', $storeId); + } + + /** + * @param string $attributeCode + * @param int $storeId + * + * @return bool + */ + private function isAttributeAllowed(string $attributeCode, int $storeId): bool + { + $allowedAttributes = $this->getRequiredAttributes($storeId); + + return empty($allowedAttributes) || in_array($attributeCode, $allowedAttributes); + } +} diff --git a/src/module-vsbridge-indexer-catalog/Model/Attributes/CategoryChildAttributes.php b/src/module-vsbridge-indexer-catalog/Model/Attributes/CategoryChildAttributes.php index 192f1114..4261e37f 100644 --- a/src/module-vsbridge-indexer-catalog/Model/Attributes/CategoryChildAttributes.php +++ b/src/module-vsbridge-indexer-catalog/Model/Attributes/CategoryChildAttributes.php @@ -11,16 +11,6 @@ */ class CategoryChildAttributes { - /** - * @const - */ - const MINIMAL_ATTRIBUTE_SET = [ - 'name', - 'is_active', - 'url_path', - 'url_key', - ]; - /** * @var CategoryConfigInterface */ @@ -37,18 +27,18 @@ public function __construct(CategoryConfigInterface $categoryConfig) } /** - * Retrieve Required children attributes for child category + * Retrieve required attributes for child category * * @param int $storeId * * @return array */ - public function getRequiredAttributes(int $storeId) + public function getRequiredAttributes(int $storeId): array { $attributes = $this->config->getAllowedChildAttributesToIndex($storeId); if (!empty($attributes)) { - $attributes = array_merge($attributes, self::MINIMAL_ATTRIBUTE_SET); + $attributes = array_merge($attributes, CategoryAttributes::MINIMAL_ATTRIBUTE_SET); return array_unique($attributes); } diff --git a/src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Category/AttributeData.php b/src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Category/AttributeData.php index 0b7af04b..733708bc 100644 --- a/src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Category/AttributeData.php +++ b/src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Category/AttributeData.php @@ -10,6 +10,7 @@ use Divante\VsbridgeIndexerCatalog\Model\ResourceModel\Category\Children as CategoryChildrenResource; use Divante\VsbridgeIndexerCore\Indexer\DataFilter; +use Divante\VsbridgeIndexerCatalog\Model\Attributes\CategoryAttributes; use Divante\VsbridgeIndexerCatalog\Model\Attributes\CategoryChildAttributes; use Divante\VsbridgeIndexerCatalog\Model\SystemConfig\CategoryConfigInterface; use Divante\VsbridgeIndexerCatalog\Api\ApplyCategorySlugInterface; @@ -45,6 +46,11 @@ class AttributeData implements AttributeDataProviderInterface */ private $childAttributes; + /** + * @var CategoryAttributes + */ + private $categoryAttributes; + /** * @var AttributeDataProvider */ @@ -93,6 +99,7 @@ class AttributeData implements AttributeDataProviderInterface * @param ProductCountResourceModel $productCountResource * @param ApplyCategorySlugInterface $applyCategorySlug * @param CategoryConfigInterface $configSettings + * @param CategoryAttributes $categoryAttributes * @param CategoryChildAttributes $categoryChildAttributes * @param DataFilter $dataFilter */ @@ -102,6 +109,7 @@ public function __construct( ProductCountResourceModel $productCountResource, ApplyCategorySlugInterface $applyCategorySlug, CategoryConfigInterface $configSettings, + CategoryAttributes $categoryAttributes, CategoryChildAttributes $categoryChildAttributes, DataFilter $dataFilter ) { @@ -111,6 +119,7 @@ public function __construct( $this->attributeResourceModel = $attributeResource; $this->childrenResourceModel = $childrenResource; $this->dataFilter = $dataFilter; + $this->categoryAttributes = $categoryAttributes; $this->childAttributes = $categoryChildAttributes; } @@ -128,13 +137,18 @@ public function addData(array $indexData, $storeId) */ $categoryIds = array_keys($indexData); - $attributes = $this->attributeResourceModel->loadAttributesData($storeId, $categoryIds); + $attributes = $this->attributeResourceModel->loadAttributesData( + $storeId, + $categoryIds, + $this->categoryAttributes->getRequiredAttributes($storeId) + ); $productCount = $this->productCountResource->loadProductCount($categoryIds); foreach ($attributes as $entityId => $attributesData) { $categoryData = array_merge($indexData[$entityId], $attributesData); $categoryData = $this->prepareParentCategory($categoryData, $storeId); - $categoryData = $this->addSortOptions($categoryData, $storeId); + $categoryData = $this->addDefaultSortByOption($categoryData, $storeId); + $categoryData = $this->addAvailableSortByOption($categoryData, $storeId); $categoryData['product_count'] = $productCount[$entityId]; $indexData[$entityId] = $categoryData; @@ -276,22 +290,45 @@ private function prepareCategory(array $categoryDTO, int $storeId) return $categoryDTO; } + /** + * @param array $category + * @param $storeId + * + * @return array + */ + private function addAvailableSortByOption(array $category, $storeId): array + { + if (!$this->categoryAttributes->canAddAvailableSortBy($storeId)) { + return $category; + } + + if (isset($category['available_sort_by'])) { + return $category; + } + + $category['available_sort_by'] = $this->settings->getAttributesUsedForSortBy(); + + return $category; + } + /** * @param array $category * @param int $storeId * * @return array */ - private function addSortOptions(array $category, $storeId) + private function addDefaultSortByOption(array $category, $storeId): array { - if (!isset($category['available_sort_by'])) { - $category['available_sort_by'] = $this->settings->getAttributesUsedForSortBy(); + if (!$this->categoryAttributes->canAddDefaultSortBy($storeId)) { + return $category; } - if (!isset($category['default_sort_by'])) { - $category['default_sort_by'] = $this->settings->getProductListDefaultSortBy($storeId); + if (isset($category['default_sort_by'])) { + return $category; } + $category['default_sort_by'] = $this->settings->getProductListDefaultSortBy($storeId); + return $category; } diff --git a/src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Product/MediaGalleryData.php b/src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Product/MediaGalleryData.php index 542eb57a..ca2d326a 100644 --- a/src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Product/MediaGalleryData.php +++ b/src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Product/MediaGalleryData.php @@ -68,7 +68,7 @@ private function canIndexMediaGallery(int $storeId) { if (null === $this->canIndexMediaGallery) { $attributes = $this->catalogConfig->getAllowedAttributesToIndex($storeId); - $this->canIndexMediaGallery = in_array('media_gallery', $attributes) || empty($attributes); + $this->canIndexMediaGallery = empty($attributes) || in_array('media_gallery', $attributes); } return $this->canIndexMediaGallery; diff --git a/src/module-vsbridge-indexer-catalog/Model/SystemConfig/CategoryConfig.php b/src/module-vsbridge-indexer-catalog/Model/SystemConfig/CategoryConfig.php index 0c05448b..82d8ff59 100644 --- a/src/module-vsbridge-indexer-catalog/Model/SystemConfig/CategoryConfig.php +++ b/src/module-vsbridge-indexer-catalog/Model/SystemConfig/CategoryConfig.php @@ -71,12 +71,20 @@ public function __construct( */ public function getAllowedAttributesToIndex(int $storeId): array { + $cacheKey = sprintf('allowed_attributes_%d', $storeId); + + if (isset($this->settings[$cacheKey])) { + return $this->settings[$cacheKey]; + } + $attributes = (string)$this->getConfigParam( CategoryConfigInterface::CATEGORY_ATTRIBUTES, $storeId ); - return $this->getAttributeCodesByIds->execute($attributes); + $this->settings[$cacheKey] = $this->getAttributeCodesByIds->execute($attributes); + + return $this->settings[$cacheKey]; } /** @@ -88,11 +96,19 @@ public function getAllowedAttributesToIndex(int $storeId): array */ public function getAllowedChildAttributesToIndex(int $storeId): array { + $cacheKey = sprintf('child_allowed_attributes_%d', $storeId); + + if (isset($this->settings[$cacheKey])) { + return $this->settings[$cacheKey]; + } + $attributes = (string)$this->getConfigParam( CategoryConfigInterface::CHILD_ATTRIBUTES, $storeId ); + $this->settings[$cacheKey] = $this->getAttributeCodesByIds->execute($attributes); + return $this->getAttributeCodesByIds->execute($attributes); }