I have a vehicle parts site and you can choose your vehicle model, so when you browse the site (for example) breaks. I only want to show breaks specific to the vehicle model stored in the cookie.

So the sudo code is something like this, when I go to any category or search:

If cookie is present

  Show custom product collection which has been filtered by Attribute stored in cookie


  Show normal product collection for category


I've tried to extend the ListProduct block to add another custom method although I can get the method to work i.e. pass data between, when I try to load a product collection it doesn't work.

Can you please point me in the correct direction for this solution. Am I doing the correct thing or is there a more simple route to take.

I've been puzzled by this for a while now and every resource on the internet i've found always uses the same example of collection and factories. Building a module to display a custom collection i.e. posts.

I really don't need that, what I need is fairly simple... in theory. Any help would be amazing.


Struggling to get the plugin working. Getting this error.

Exception #0 (InvalidArgumentException): Plugin class Plan2pixel\VehicleChoice\Plugin\Product\NewCollectionPlugin doesn't exist

But the file is here: /app/code/Plan2pixel/VehicleChoice/Plugin/Product/NewCollectionPlugin.php'

This is the di.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Layer">
        <plugin name="plugin_newcollection" type="Plan2pixel\VehicleChoice\Plugin\Product\NewCollectionPlugin" />


namespace Plan2pixel\VehicleChoice\Plugin\Productcollection\NewCollectionPlugin;

use Magento\Catalog\Model\Layer as CatalogLayer;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;

class NewCollectionPlugin {
    public function aroundPrepareProductCollection(CatalogLayer $subject, \Closure $proceed, $collection)

        $cookie_helper  = $this->helper('RoughTrax\Session\Helper\Cookie');

        $stored_vehicle = $cookie_helper->get("roughtrax_store_vehicle_date_set");


        if ($stored_vehicle) {

            $collection->setPageSize(3); // fetching only 3 products just to prove the code works, will adjust after.

        return $this;

1 Answer 1


The easiest way to adapt the product collection in the catalog is to write a plugin on the Magento\Catalog\Model\Layer's prepareProductCollection() method. Use an around plugin as the method returns itself (so after won't work) and you need the Catalog Category Collection Filter to do its work first.

You check the cookie there in your plugin. If no cookie has been set, you just do nothing... If a cookie has been set, you can alter the product collection object's filters or change the query (Select object) directly.

namespace MyCompany\VehicleCatalog\Plugin\Catalog;

use Magento\Catalog\Model\Layer as CatalogLayer;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;

class LayerPlugin {
    public function aroundPrepareProductCollection(CatalogLayer $subject, \Closure $proceed, $collection)
        if (/*cookie is present and has a valid value*/) {
            $collection->.../* Do magic... */
        return $this;
  • Can you edit and add to your question what you have right now? What is in your di.xml and what is in de plugin?
    – 7ochem
    Dec 20, 2017 at 9:45
  • Sorry, new to stackexchange thanks for your patience and quick responses. Will mark as correct asap. Dec 20, 2017 at 9:52
  • Your namespace in the plugin class file is wrong. It should reflect the path exactly. And remove the last part \NewCollectionPlugin
    – 7ochem
    Dec 20, 2017 at 10:31
  • Yes sorry, everything working now with the plugin side of things. I can''t upvote not enough rep, but have saved answer. Thank you. Dec 21, 2017 at 12:17
  • You're welcome!
    – 7ochem
    Dec 21, 2017 at 12:18

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.