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
else
Show normal product collection for category
end
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.
[Edit]
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" />
</type>
</config>
NewCollectionPlugin.php
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");
$proceed($collection);
if ($stored_vehicle) {
$collection->setPageSize(3); // fetching only 3 products just to prove the code works, will adjust after.
}
return $this;
}
}