Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1858 Optimizer preview for inactive category #1859

Open
wants to merge 1 commit into
base: 2.9.x
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalogOptimizer
* @author Dmytro ANDROSHCHUK <[email protected]>
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCatalogOptimizer\Ui\Component\Product\Form\Categories;

use Magento\Framework\Data\OptionSourceInterface;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\Category as CategoryModel;

/**
* Options tree for "Categories" field
*
* @category Smile
* @package Smile\ElasticsuiteCatalogOptimizer
* @author Dmytro ANDROSHCHUK <[email protected]>
*/
class Options implements OptionSourceInterface
{
/**
* @var CategoryCollectionFactory
*/
protected $categoryCollectionFactory;

/**
* @var RequestInterface
*/
protected $request;

/**
* @var array
*/
protected $categoriesTree;

/**
* @param CategoryCollectionFactory $categoryCollectionFactory Category Collection Factory.
* @param RequestInterface $request Request.
*/
public function __construct(
CategoryCollectionFactory $categoryCollectionFactory,
RequestInterface $request
) {
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->request = $request;
}

/**
* {@inheritdoc}
*/
public function toOptionArray()
{
return $this->getCategoriesTree();
}

/**
* Retrieve categories tree
*
* @return array
*/
protected function getCategoriesTree()
{
if ($this->categoriesTree === null) {
$storeId = $this->request->getParam('store');
/* @var $matchingNamesCollection Collection */
$matchingNamesCollection = $this->categoryCollectionFactory->create();

$matchingNamesCollection->addAttributeToSelect('path')
->addAttributeToFilter('entity_id', ['neq' => CategoryModel::TREE_ROOT_ID])
->setStoreId($storeId);

$shownCategoriesIds = [];

/** @var \Magento\Catalog\Model\Category $category */
foreach ($matchingNamesCollection as $category) {
foreach (explode('/', $category->getPath()) as $parentId) {
$shownCategoriesIds[$parentId] = 1;
}
}

$collection = $this->categoryCollectionFactory->create();

$collection->addAttributeToFilter('entity_id', ['in' => array_keys($shownCategoriesIds)])
->addAttributeToSelect(['name', 'is_active', 'parent_id'])
->setStoreId($storeId);

$categoryById = [
CategoryModel::TREE_ROOT_ID => [
'value' => CategoryModel::TREE_ROOT_ID,
],
];

foreach ($collection as $category) {
// Don't display inactive categories.
if (!$category->getIsActive()) {
continue;
}

foreach ([$category->getId(), $category->getParentId()] as $categoryId) {
if (!isset($categoryById[$categoryId])) {
$categoryById[$categoryId] = ['value' => $categoryId];
}
}

$categoryById[$category->getId()]['is_active'] = $category->getIsActive();
$categoryById[$category->getId()]['label'] = $category->getName();
$categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];
}

$this->categoriesTree = $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];
}

return $this->categoriesTree;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@

<field name="category_preview">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Magento\Catalog\Ui\Component\Product\Form\Categories\Options</item>
<item name="options" xsi:type="object">Smile\ElasticsuiteCatalogOptimizer\Ui\Component\Product\Form\Categories\Options</item>
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Category Preview</item>
<item name="componentType" xsi:type="string">field</item>
Expand Down