Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing direct usage of Layer to prepare a product collection which th… #356

Merged
merged 2 commits into from
Mar 20, 2017

Conversation

romainruaud
Copy link
Collaborator

…ey did not instantiate themselves.

This is mostly related to the Call to undefined method Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor::addSortFilterParameters() error.

This error occurs when Magento uses the Layer object as a filter, to prepare an external product collection.

This can be seen especially on Magento\Catalog\Block\Navigation :

$productCollection = $this->_productCollectionFactory->create(); $this->_catalogLayer->prepareProductCollection($productCollection);

the $productCollection is created by the standard product collection factory. Therefore it is not possible to use ElasticSuite method on it when the layer calls the prepareProductCollection.

This can be fixed by switching the factory used to the CatalogSearch one, and will then work properly.

Imho, since we cannot ensure which type of collection is sent to the Layer, and since prepareProductCollection is a public method, we should go further on this one.

Let me know @afoucret

@afoucret
Copy link
Contributor

The automatic instantiation of product collection using \Magento\Catalog\Model\Layer\Category\ItemCollectionProvider should be managed.

I think it can be useful in some case.

@romainruaud
Copy link
Collaborator Author

romainruaud commented Mar 20, 2017

Hello, I just tested the PR with this block :

class Test extends \Magento\Framework\View\Element\Template
{
    private $catalogLayer;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        array $data
    ) {
        $this->catalogLayer = $layerResolver->get();
        parent::__construct($context, $data);
    }

    protected function _prepareLayout()
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/rorua.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $collection = $this->catalogLayer->getProductCollection();
        $collection->addIsInStockFilter();

        $logger->info(" Class : " . get_class($collection));

        return parent::_prepareLayout();
    }
}

Which produces no error and outputs :

2017-03-20T15:00:41+00:00 INFO (6):  Class : Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection\Interceptor

@afoucret afoucret merged commit c26674e into Smile-SA:2.3.x Mar 20, 2017
@romainruaud romainruaud deleted the fix_erratic-layer-usage branch April 19, 2017 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants