Magento 2 – How to Override Abstract Class for Product Category Indexing Issue

categorycategory-productsindexmagento-2.1.3magento2

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;
    }
}

Best Answer

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.

Related Topic