Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Configure category attributes for export - fix excluding attributes from
Browse files Browse the repository at this point in the history
category.
  • Loading branch information
Agata Firlejczyk committed Apr 30, 2020
1 parent 3685526 commit 48c9d46
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Divante\VsbridgeIndexerCatalog\Model\Attributes;

use Divante\VsbridgeIndexerCatalog\Model\SystemConfig\CategoryConfigInterface;

/**
* Class CategoryAttributes
*/
class CategoryAttributes
{
/**
* @const
*/
const MINIMAL_ATTRIBUTE_SET = [
'name',
'is_active',
'url_path',
'url_key',
];

/**
* @var CategoryConfigInterface
*/
private $config;

/**
* CategoryChildAttributes constructor.
*
* @param CategoryConfigInterface $categoryConfig
*/
public function __construct(CategoryConfigInterface $categoryConfig)
{
$this->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@
*/
class CategoryChildAttributes
{
/**
* @const
*/
const MINIMAL_ATTRIBUTE_SET = [
'name',
'is_active',
'url_path',
'url_key',
];

/**
* @var CategoryConfigInterface
*/
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +46,11 @@ class AttributeData implements AttributeDataProviderInterface
*/
private $childAttributes;

/**
* @var CategoryAttributes
*/
private $categoryAttributes;

/**
* @var AttributeDataProvider
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -102,6 +109,7 @@ public function __construct(
ProductCountResourceModel $productCountResource,
ApplyCategorySlugInterface $applyCategorySlug,
CategoryConfigInterface $configSettings,
CategoryAttributes $categoryAttributes,
CategoryChildAttributes $categoryChildAttributes,
DataFilter $dataFilter
) {
Expand All @@ -111,6 +119,7 @@ public function __construct(
$this->attributeResourceModel = $attributeResource;
$this->childrenResourceModel = $childrenResource;
$this->dataFilter = $dataFilter;
$this->categoryAttributes = $categoryAttributes;
$this->childAttributes = $categoryChildAttributes;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/**
Expand All @@ -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);
}

Expand Down

0 comments on commit 48c9d46

Please sign in to comment.