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

Enforce reindexing when category is set to virtual. #488

Merged
merged 2 commits into from
Aug 10, 2017
Merged
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,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