Skip to content

Commit

Permalink
Merge pull request #488 from romainruaud/fix_virtual_category_reindex
Browse files Browse the repository at this point in the history
Enforce reindexing when category is set to virtual.
  • Loading branch information
afoucret authored Aug 10, 2017
2 parents 9d3f4a5 + 6c99385 commit 16ec29d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteVirtualCategory
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2017 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteVirtualCategory\Plugin\Catalog\Category;

/**
* Trigger reindexing for category when switched from Standard to "Virtual Category"
*
* @category Smile
* @package Smile\ElasticsuiteVirtualCategory
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class ReindexOnChange
{
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
private $indexerRegistry;

/**
* ReindexOnChange constructor.
*
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry Indexer Registry
*/
public function __construct(\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry)
{
$this->indexerRegistry = $indexerRegistry;
}

/**
* Enforce reindexing of path ids if the category has just been set to 'is_virtual_category' = true.
*
* @param \Magento\Catalog\Model\Category $category The category
* @param \Closure $proceed The Category::reindex() method
*/
public function aroundReindex(\Magento\Catalog\Model\Category $category, \Closure $proceed)
{
$proceed();

// Reindex only if attached product list has changed.
// This prevent reindexing if the category is just created and set to virtual.
if ($category->dataHasChangedFor('is_virtual_category') && ($category->getIsChangedProductList() === true)) {
if (((bool) $category->getIsVirtualCategory() === true) && ($category->getId())) {
if (!$this->getIndexer()->isScheduled()) {
// Remove default root category (1) and Store Root.
$this->getIndexer()->reindexList(array_slice($category->getPathIds(), 2));
}
}
}
}

/**
* Retrieve Category/Product indexer.
*
* @return \Magento\Framework\Indexer\IndexerInterface
*/
private function getIndexer()
{
return $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Category\Product::INDEXER_ID);
}
}
5 changes: 5 additions & 0 deletions src/module-elasticsuite-virtual-category/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
<plugin name="smile_elasticsuite_virtual_categories_save_products_positions"
type="\Smile\ElasticsuiteVirtualCategory\Plugin\Catalog\Category\SaveProductsPositions" />
</type>

<type name="\Magento\Catalog\Model\Category">
<plugin name="smile_elasticsuite_virtual_categories_reindex_on_change"
type="\Smile\ElasticsuiteVirtualCategory\Plugin\Catalog\Category\ReindexOnChange" />
</type>

<type name="Smile\ElasticsuiteCatalog\Model\Product\Indexer\Fulltext\Datasource\CategoryData">
<arguments>
Expand Down

0 comments on commit 16ec29d

Please sign in to comment.