Magento 2: Show correct price on the Recently Viewed Block for the Configurable products

With Magento 2.4.4, there is an issue for showing up correct price of configurable product inside Recently Viewed Block.

Posted on October 13, 2022

After upgrading to Magento 2.4.4, there is an issue for showing correct price for configurable products inside Recently Viewed Block. Sometime it will never show up or it will show up with 0.00. I spent a lot of time to figure out the causes and solved it finally. I want to share the solution with you if you have the similar issue.

I spent some time to read “widget_recently_viewed.xml” under ‘vendor/magento/module-catalog/view/frontend/ui_component/’. All data will be provided with ‘\Magento\Catalog\Ui\DataProvider\ Product\Listing\DataProvider’, which is defined under [dataSource] => [dataProvider] class attribute inside xml file.

For the priceInfo, the php code is at ‘vendor/magento/module-catalog/ Ui/DataProvider/Listing/Collector/Price.php’. In order to populate all prices, for example, ‘regular_price’, ‘maximum_price’, ‘minimum_price’. We have to update the Code as following:


public function collect(ProductInterface $product, ProductRenderInterface $productRender)
{
        $priceInfo = $productRender->getPriceInfo();

        if (!$productRender->getPriceInfo()) {
            /** @var PriceInfoInterface $priceInfo */
            $priceInfo = $this->priceInfoFactory->create();
    }

    $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom222.log');
    $logger = new \Zend_Log();
    $logger->addWriter($writer);

    $id = $product->getId();
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $p = $objectManager->create('Magento\Catalog\Model\ProductFactory')->create()->load($id);   
    $typeId = $p->getTypeId();
    $logger->info('typeId: ' . $p->getTypeId());

        $priceInfo->setFinalPrice(
            $product
                ->getPriceInfo()
                ->getPrice('final_price')
                ->getAmount()
                ->getValue()
        );
        $priceInfo->setMinimalPrice(
            $product
                ->getPriceInfo()
                ->getPrice('final_price')
                ->getMinimalPrice()
                ->getValue()
        );
        $priceInfo->setRegularPrice(
            $product
                ->getPriceInfo()
                ->getPrice('regular_price')
                ->getAmount()
                ->getValue()
        );
        $priceInfo->setMaxPrice(
            $product
                ->getPriceInfo()
                ->getPrice('final_price')
                ->getMaximalPrice()
                ->getValue()
        );

    if($typeId == 'configurable') {
            $max = 0;
            $min = 999999;
            $_children = $p->getTypeInstance()->getUsedProducts($p);
            foreach($_children as $c) {
                $price = $c->getFinalPrice();
                if($price > $max) {
                    $max = $price;
                }

                if($price < $min) {
                    $min = $price;
                }

               if($max < $min) {
                    $temp = $max;
                    $max = $min;
                    $min = $temp
                }
            }

            $priceInfo->setFinalPrice($min);
            $priceInfo->setMinimalPrice($min);
            $priceInfo->setRegularPrice($min);
            $priceInfo->setMaxPrice($max);
        $logger->info('min:' . $min);
    }

        $this->formattedPriceInfoBuilder->build(
            $priceInfo,
            $productRender->getStoreId(),
            $productRender->getCurrencyCode()
        );

        $productRender->setPriceInfo($priceInfo);
    }
}

The JavaScript file under ‘vendor/magento/module-catalog/view/base/web/js/product/list/columns/final-price.js’ will use the aforementioned php data to populate the UI Compoent, widget_recently_viewed.xml.

As we must follow the Magento development rule, we should never change any core file under vendor folder. So it’s better to create reference inside any di.xml and overwrite it with your own class.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <preference for="Magento\Catalog\Ui\DataProvider\Product\Listing\Collector\Price" 
                 type="Your_Company\Module_name\Plugin\PreferenceForPrice"/>
</config>


comments powered by Disqus