product price is calculated dynamically based on certain conditions. while sorting with price it takes only default price not dynamically calculated price. the same issue with the price filter.
I can able to set the calculated price for product using event catalog_block_product_list_collection observer but not able to get products ordered by dynamic price.
<event name="catalog_block_product_list_collection">
<observer name="product_price_change" instance="[vendor]\[module]\Observer\ProductCollectionDynamicPrice" shared="false" />
</event>
Observer - [vendor][module]\Observer\ProductCollectionDynamicPrice
<?php
namespace [vendor]\[module]\Observer;
use Magento\Framework\Event\ObserverInterface;
class ProductCollectionDynamicPrice implements ObserverInterface
{
/**
* set dynamic price
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$productCollection = $observer->getEvent()->getCollection();
if ($productCollection instanceof \Magento\Framework\Data\Collection) {
$productCollection->load();
$this->calculatePrice($productCollection);
}
return $this;
}
/**
* set dynamic calculated price to product collection
*
* @param ProductCollection $collection
* @return $this
*/
public function calculatePrice($collection) {
foreach ($collection as $key => $item) {
$price = 100; // here my cuustom logic to calculate dynamic price based on conditions
$item->setMinPrice($price);
}
return $collection;
}
}
If I echoing min_price then able to get updated min_price but not able to get correct product orders collection sort by price.
Please anyone can help for the issue?