Magento 2: How to Override Abstract Class for Product Category Indexing Issue?
-
06-10-2020 - |
Question
I have 2000 products which is assigned to multiple categories.
When i run
php bin/magento indexer:reindex catalog_category_product
In catalog_category_product_index
total record count is 1700.
So on frontend remaining 300 products are not displaying under the category, but if I search that product then it displays in result.
From Admin when I again Select All Enabled product & Change Status of them to Enable again then it displays correct count & number of products under the category.
What will be root cause or any alternative solution?
https://github.com/magento/magento2/issues/8018
[UPDATE]
Issue on magento\vendor\magento\module-catalog\Model\Indexer\Category\Product\AbstractAction.php files function isRangingNeeded
magento\app\code\Custom\Catalog\etc\di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Model\Indexer\Category\Product\AbstractAction" type="Custom\Catalog\Model\Indexer\Category\Product\AbstractAction" />
</config>
magento\app\code\Custom\Catalog\Model\Indexer\Category\Product\AbstractAction.php
namespace Custom\Catalog\Model\Indexer\Category\Product;
abstract class AbstractAction extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractAction {
/**
* Check whether select ranging is needed
*
* @return bool
*/
public function isRangingNeeded() {
/*
* PATCH for select query limiting bug See https://github.com/magento/magento2/issues/8018
*/
return false;
}
}
May be abstract class override or extend issue?
[UPDATE 2]
magento\app\code\Custom\Catalog\etc\di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Indexer\Category\Product\AbstractAction">
<plugin name="custom_catalog_product" type="Custom\Catalog\Plugin\Model\Indexer\Category\Product\AbstractAction" />
</type>
</config>
magento\app\code\Custom\Catalog\Plugin\Model\Indexer\Category\Product\AbstractAction.php
namespace Custom\Catalog\Plugin\Model\Indexer\Category\Product;
/**
* Class AbstractAction
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AbstractAction
{
/**
* Check whether select ranging is needed
*
* @return bool
*/
public function afterIsRangingNeeded() {
/*
* PATCH for select query limiting bug See https://github.com/magento/magento2/issues/8018
*/
return false;
}
}
Solution
magento\app\code\Custom\Catalog\etc\di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Model\Indexer\Category\Product\Action\Full" type="Custom\Catalog\Model\Indexer\Category\Product\Action\Full" />
</config>
magento\app\code\Custom\Catalog\Model\Indexer\Category\Product\Action\Full.php
namespace Custom\Catalog\Model\Indexer\Category\Product\Action;
/**
* Class AbstractAction
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Full extends \Magento\Catalog\Model\Indexer\Category\Product\Action\Full {
/**
* Check whether select ranging is needed
*
* @return bool
*/
public function isRangingNeeded() {
return false; // By default true, due to indexing issue making it false
}
}
Run
php bin/magento indexer:reindex catalog_category_product
It will solve the issue for now until Magento new release.
OTHER TIPS
Overriding does not work as a real overriding but goes throught plugins.
You can follow the tutorial of inchoo for overriding classes as it worked for me. It looks like you did not declare the plugin in your di.xml. Moreover, the function should be something like beforeIsRangingNeeded() or afterIsRangingNeeded().
In your case, the action isRangingNeeded() is set in abstractAction to return a default value. People on the link you quoted explain that you need to write directly in the core the bugfix as "overriding" works the way I explained.
A cleaner solution would be to create a new indexer that would replace the one that bugs.
This is my patch example. Above Ankit Shah' answer is great. But, in that case, we should create new module.xml file and register this patch as if it 's a new module.
If not, it will occur critical bug on re-indexing process.
Especially, site is just live production one, my answer is just more convenient answer.
We can easily add these two files on original Modules folder.
app\code\Modules\Catalog\etc\di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Model\Indexer\Category\Product\Action\Full" type="Modules\Catalog\Model\Indexer\Category\Product\Action\Full" />
</config>
app\code\Modules\Catalog\Model\Indexer\Category\Product\Action\Full.php
<?php
namespace Modules\Catalog\Model\Indexer\Category\Product\Action;
/**
* Class AbstractAction
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Full extends \Magento\Catalog\Model\Indexer\Category\Product\Action\Full {
public function isRangingNeeded() {
return false; // It was "True" as default setting.
}
}
Then, we should run this command.
php bin/magento cache:clean
php bin/magento indexer:reindex
Finally, we got whole 2000 products on our category page instead of previous 340 products on frontend page.
Also, the "Product category" tab on index management is just updated from date of version upgraded to current indexing date and time.
Great experience!
Hope this will help many developers and owners.