0

Just wanted to know, how can we use virtualType programmatically in magento 2 custom script file ?

Actually I am building api for layered navigation and I am getting following error.

Fatal error: Uncaught Error: Cannot instantiate interface Magento\Catalog\Model\Layer\FilterableAttributeListInterface in /var/www/html/eoutlet/public_html/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php:111 Stack trace: #0 /var/www/html/eoutlet/public_html/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php(108): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Magento\\Catalog...', Array) #1 /var/www/html/eoutlet/public_html/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php(150): Magento\Framework\ObjectManager\Factory\Compiled->create('Magento\\Catalog...') #2 /var/www/html/eoutlet/public_html/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php(79): Magento\Framework\ObjectManager\Factory\Compiled->get('Magento\\Catalog...') #3 /var/www/html/eoutlet/public_html/lib/internal/Magento/Framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Compiled->create('Amasty\\Shopby\\M...') #4 /var/www/html/eoutl in /var/www/html/eoutlet/public_html/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php on line 111 

FYI - I am using amasty shopby extension and found that, it is using virtualType in di.xml file.

<virtualType name="categoryFilterList" type="Amasty\Shopby\Model\Layer\FilterList">
 <arguments>
        <argument name="filters" xsi:type="array">
            <item name="attribute" xsi:type="string">Amasty\Shopby\Model\Layer\Filter\Attribute</item>
            <item name="price" xsi:type="string">Amasty\Shopby\Model\Layer\Filter\Price</item>
            <item name="decimal" xsi:type="string">Amasty\Shopby\Model\Layer\Filter\Decimal</item>
            <item name="category" xsi:type="string">Amasty\Shopby\Model\Layer\Filter\Category</item>
        </argument>
        <argument name="place" xsi:type="string">sidebar</argument>
    </arguments>
</virtualType>

I am implementing following code to get the result, but getting above error

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterableAttributes = $objectManager->create('\Magento\Catalog\Model\Layer\Category\FilterableAttributeList'::class);
$filterTypes = [
            'attribute'=>\Amasty\Shopby\Model\Layer\Filter\Attribute::class,
            'category'=>\Amasty\Shopby\Model\Layer\Filter\Category::class,
            'decimal'=>\Amasty\Shopby\Model\Layer\Filter\Decimal::class,
            'price'=>\Amasty\Shopby\Model\Layer\Filter\Price::class
        ];

$layerResolver = $objectManager->get('\Amasty\Shopby\Model\Resolver'::class);
$filterList = $objectManager->get(
'\Amasty\Shopby\Model\Layer\FilterList'::class,
    [
       'filterableAttributes' => $filterableAttributes,
       'filters'=>$filterTypes                   
    ]
); 

Any help would be appriciated. Thanks in advance.

0

Create custom layered navigation

https://webkul.com/blog/how-to-create-layered-navigation-on-custom-page-in-magento-2/

|improve this answer|||||

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.