Magento 2 – Add Search by Category in Advanced Search

advanced-searchcategory-attributemagento-2.1magento2

I tried the code of @Nitesh here Add search by category by Nitesh

and I got this error:

Notice: Undefined property:
Magento\CatalogSearch\Model\Advanced::$_objectManager in
/opt/lampp/htdocs/mwh-das-original/vendor/magento/module-catalog-search/Model/Advanced.php
on line 269

enter image description here

and also notice that category list doubled
enter image description here

Best Answer

There is error in the @Nitesh code, because the Magento\CatalogSearch\Model\Advanced class has no attribute _objectManager. To make it work you should add this attribute in a class which rewrites Magento\CatalogSearch\Model\Advanced, I think you have it because @Nitesh says:

To add this, you will need to override the following files:

vendor/magento/core/module-catalog-search/Block/Advanced/Form.php
vendor/magento/core/module-catalog-search/Model/Advanced.php
vendor/magento/module-catalog-search/view/frontend/templates/advanced/form.phtml

So, just declare the __construct method and add the ObjectManager instance and attribute, like:

use Magento\Catalog\Model\Config;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection as ProductCollection;
use Magento\CatalogSearch\Model\ResourceModel\AdvancedFactory;
use Magento\Directory\Model\CurrencyFactory;
use Magento\Eav\Model\Entity\Attribute as EntityAttribute;
use Magento\Framework\Model\Context;
use Magento\Framework\Registry;
use Magento\Store\Model\StoreManagerInterface;

class YourRewriteAdvancedClassName extends \Magento\CatalogSearch\Model\Advanced
{
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @param Context $context
     * @param Registry $registry
     * @param AttributeCollectionFactory $attributeCollectionFactory
     * @param Visibility $catalogProductVisibility
     * @param Config $catalogConfig
     * @param CurrencyFactory $currencyFactory
     * @param ProductFactory $productFactory
     * @param StoreManagerInterface $storeManager
     * @param ProductCollectionFactory $productCollectionFactory
     * @param AdvancedFactory $advancedFactory
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        AttributeCollectionFactory $attributeCollectionFactory,
        Visibility $catalogProductVisibility,
        Config $catalogConfig,
        CurrencyFactory $currencyFactory,
        ProductFactory $productFactory,
        StoreManagerInterface $storeManager,
        ProductCollectionFactory $productCollectionFactory,
        AdvancedFactory $advancedFactory,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $registry,
            $attributeCollectionFactory,
            $catalogProductVisibility,
            $catalogConfig,
            $currencyFactory,
            $productFactory,
            $storeManager,
            $productCollectionFactory,
            $advancedFactory,
            $data
        );
        $this->_objectManager = $objectManager;
    }

    // Here is you custom method from question
}

PS: update the class which rewrites Magento\CatalogSearch\Model\Advanced class using this code, but do not overwrite it completely.

Related Topic