-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #488 from romainruaud/fix_virtual_category_reindex
Enforce reindexing when category is set to virtual.
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/module-elasticsuite-virtual-category/Plugin/Catalog/Category/ReindexOnChange.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters