Create the Custom Module:
Create a custom module in your Magento 2 installation. Let's call it Custom_Catalog. You'll need to create the necessary module structure with files and folders:
app/code/Low/Price/registration.php
app/code/Low/Price/etc/module.xml
Declare the Module:
In registration.php, register your module:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Custom_Catalog', __DIR__);
Module Configuration:
In etc/module.xml, define your module:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Custom_Catalog" setup_version="1.0.0"/>
</config>
Create the Observer:
In your custom module, create an observer to modify the product collection on category pages. Let's call it PriceFilterObserver.
File: app/code/Low/Price/Observer/CatalogProductListCollectionObserver.php
<?php
declare(strict_types=1);
namespace Low\Price\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
/**
*COMMENT
*/
class CatalogProductListCollectionObserver implements ObserverInterface
{
/**
* @param EventObserver $observer
* @return $this|void
*/
public function execute(EventObserver $observer)
{
$productCollection = $observer->getEvent()->getCollection();
$productCollection->addAttributeToFilter('price', ['lteq' => 10]);
return $this;
}
}
Declare the Observer:
Create a file app/code/Low/Price/etc/events.xml to declare the observer:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_block_product_list_collection">
<observer name="vendor_product_list_filter" instance="Simple\Lowprice\Observer\CatalogProductListCollectionObserver"/>
</event>
</config>
Enable the Module:
Enable the module and clear the cache as explained in the previous example:
php bin/magento module:enable Low_Price
php bin/magento setup:upgrade
php bin/magento cache:clean
After implementing these steps, the observer will be triggered whenever the product collection is loaded on a category page. It will modify the collection to include only products with prices below or equal to $10.
On category page products that have prices below 10$ in magento2
Reviewed by Technical
on
August 15, 2023
Rating:

No comments: