I extend the collection query using event 'catalog_block_product_list_collection
'. Query displaying proper data, as well as showing proper record in product list page.
But same collection is not loading for layer & toolbar section.
here is my code
events.xml
<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="review" instance="PR\Catalog\Observer\CatalogBlockProductCollectionBeforeToHtmlObserver" shared="false" />
</event>
</config>
CatalogBlockProductCollectionBeforeToHtmlObserver.php
namespace PR\Catalog\Observer;
use Magento\Framework\Event\ObserverInterface;
class CatalogBlockProductCollectionBeforeToHtmlObserver implements ObserverInterface
{
/**
* Review model
*
* @var \Magento\Review\Model\ReviewFactory
*/
protected $_vendorProductFactory;
public function execute(\Magento\Framework\Event\Observer $observer)
{
$productCollection = $observer->getEvent()->getCollection();
if ($productCollection instanceof \Magento\Framework\Data\Collection) {
$productCollection->getSelect()->join(array(
'vendor_product' => 'pr_vendor_product'),
"e.entity_id = vendor_product.marketplace_product_id and vendor_product.status = 1",
array('vendor_product.*')
);
$productCollection->getSelect()->group('marketplace_product_id');
}
}
}
Best Answer
I successfully applied my custom product collection on category page's layered navigation and toolbar.
For example, I am fetching the collection of those products whose price is less than 100.
Step 1: Add below code snippet
Step 2: Create plugin for product collection
Step 3: Create plugin for toolbar