1

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

0

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.

  • @JeraldLaxamana I'm glad to help you. – Siarhey Uchukhlebau Jun 15 '17 at 8:33
  • hi @Siarhey one last thing. can you help to override the Form.php? i created module and the Advanced.php is working. but when i try to override Form.php advanced search page is blank. – Jerald Laxamana Jun 15 '17 at 9:58
  • @JeraldLaxamana Sure, but please ask another question and add full code (or link on github) – Siarhey Uchukhlebau Jun 15 '17 at 10:02
  • hi @Siarhey you can check github.com/nihachi/AddSearchByCategory . sorry im newbie creating module also in backend. – Jerald Laxamana Jun 15 '17 at 10:12
  • @JeraldLaxamana Ok, I’ll see how i can help you when i’ll have free time. – Siarhey Uchukhlebau Jun 15 '17 at 10:27
0

Although its not good to use object manager but if you want to you need to initiate it before.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get('Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.