Uncaught Error: Call to undefined method - Magento 2.3.3
-
17-03-2021 - |
Question
I tried find a solution to this magento 2 theme problem. How should be edit the theme file?
PHP Fatal error: Uncaught Error: Call to undefined method Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\Interceptor::getCollectionClone() in app/code/custom_theme/LayeredAjax/view/frontend/templates/layer/filter.phtml:15
Stack trace:
#0 vendor/magento/framework/View/TemplateEngine/Php.php(59): include()
#1 vendor/magento/framework/View/Element/Template.php(271): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\LayeredNavigation\Block\Navigation\FilterRenderer\Interceptor), '/home/npartsco/...', Array)
#2 vendor/magento/framework/View/Element/Template.php(301): Magento\Framework\View\Element\Template->fetchView('/home/npartsco/...')
#3 vendor/magento/module-layered-navigation/Block/Navigation/FilterRenderer.php(29): Magento\Framework\View\Element\Template->_toHtml()
#4 vendor/magento/framework/Interception/Interceptor.php(58): Mage in app/code/custom_theme/LayeredAjax/view/frontend/templates/layer/filter.phtml on line 15
line 12-23
<?php
$productCollection = $filter->getLayer()->getProductCollection();
$productCollectionClone = $productCollection->getCollectionClone();
$collection = $productCollectionClone
->removeAttributeSearch(['price.from', 'price.to']);
$min = $collection->getMinPrice();
$max = $collection->getMaxPrice();
list($from, $to) = $requestValue ? explode('-', $requestValue) : [$min, $max];
?>
Solution
I don't understand why do you want clone of your collection. But still you can try php object cloning
Instead of which is I guess not available in Magento:
$productCollection = $filter->getLayer()->getProductCollection();
$productCollectionClone = $productCollection->getCollectionClone();
Try this one:
$productCollection = $filter->getLayer()->getProductCollection();
$productCollectionClone = clone $productCollection;
OTHER TIPS
The problem is that for some reason, the product collection class
\Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection
is not overrided by the the one from Mageplaza
Mageplaza\LayeredNavigation\Model\ResourceModel\Fulltext\Collection
which is containing the methods not found getCollectionClone(), removeAttributeSearch(), etc
In our case, it seems to be related to the fact that we use ElasticSearch7 on Magento 2.4.1. The di.xml file from Mageplaza_LayeredNavigation is only set up for ElasticSearch6.
The proper way to fix it would be to create a custom module to add the missing configuration, but here is a quick fix explaining what to do. Up to you to implement it the way it suits you.
So you want to edit / override this file...
app/code/Mageplaza/LayeredNavigation/etc/di.xml
... and basically just duplicate and edit every block from this :
<arguments>
<argument name="factories" xsi:type="array">
<item name="elasticsearch6" xsi:type="object">Mageplaza\LayeredNavigation\Model\ResourceModel\Fulltext\CollectionFactory</item>
</argument>
</arguments>
to this :
<arguments>
<argument name="factories" xsi:type="array">
<item name="elasticsearch6" xsi:type="object">Mageplaza\LayeredNavigation\Model\ResourceModel\Fulltext\CollectionFactory</item>
<item name="elasticsearch7" xsi:type="object">Mageplaza\LayeredNavigation\Model\ResourceModel\Fulltext\CollectionFactory</item>
</argument>
</arguments>
Hope it can help someone ;-)