5

I have added extra price in Advance pricing in Admin. enter image description here

Everything is working well expect the data for the Recently view products which comes from Local Storage > product_data_storage

How to add that extra price in Local Storage > product_data_storage for the Frontend?

NOTE :

  • The frontend product repository is a storage service that uses the local cache to get product information without making additional requests to the server. The product information it provides is useful for optimal mini-cart, widgets, and checkout customizations.

The product data storage (product_data_storage)

  • The frontend product repository uses the product_data_storage section of the data storage cache as its data source. This section is responsible for holding all product data that come from the server when a customer visits a product page.

1 Answer 1

3
+100

Check this file vendor/magento/module-catalog/CustomerData/ProductsRenderInfoSection.php In this file, they are calling getList method which processing on product's data, And based on it Magento adding product_data_storage and for the price, Magento has used vendor/magento/module-catalog/Ui/DataProvider/Product/Listing/Collector/Price.php and vendor/magento/module-tax/Ui/DataProvider/Product/Listing/Collector/Tax.php but it is not updating price on the front side.

If you want to change full data of product_data_storage you can call your class instead of Magento\Catalog\CustomerData\ProductsRenderInfoSection as Magento has called in vendor/magento/module-catalog/etc/frontend/di.xml file like below.

<type name="Magento\Customer\CustomerData\SectionPool">
    <arguments>
        <argument name="sectionSourceMap" xsi:type="array">
            <item name="recently_viewed_product" xsi:type="string">Magento\Catalog\CustomerData\RecentlyViewedProductsSection</item>
            <item name="recently_compared_product" xsi:type="string">Magento\Catalog\CustomerData\RecentlyComparedProductsSection</item>
            <item name="product_data_storage" xsi:type="string">Magento\Catalog\CustomerData\ProductsRenderInfoSection</item>
        </argument>
    </arguments>
</type>

I have added some code with an extension attribute to add your custom data in product_data_storage. I have added my code below.

Creat extension_attributes.xml page in Your module Vendor/Module/etc/extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\ProductRenderInterface">
        <attribute code="kunj_data" type="string[]"/>
    </extension_attributes>
</config>

Now, we need to create di.xml in Vendor/Module/etc/frontend/di.xml with code.

<?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\Ui\DataProvider\Product\ProductRenderCollectorComposite">
        <arguments>
            <argument name="productProviders" xsi:type="array">
                <item name="kunj_data" xsi:type="object">\Kunj\Testing\Ui\DataProvider\Product\Listing\Collector\KunjData</item>
            </argument>
        </arguments>
    </type>
</config>

Now, need to create KunjData.php at Vendor/Module/Ui/DataProvider/Product/Listing/Collector/KunjData.php

<?php

namespace Kunj\Testing\Ui\DataProvider\Product\Listing\Collector;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Ui\DataProvider\Product\ProductRenderCollectorInterface;
use Magento\Catalog\Api\Data\ProductRenderInterface;
use Magento\Catalog\Api\Data\ProductRenderExtensionFactory;

class KunjData implements ProductRenderCollectorInterface
{

    /**
     * @var \Magento\Catalog\Api\Data\ProductRender\ProductRenderExtensionInterfaceFactory
     */
    private $productRenderExtensionFactory;

    /**
     * @param \Magento\Catalog\Api\Data\ProductRenderExtensionFactory $productRenderExtensionFactory
     */
    public function __construct(
        ProductRenderExtensionFactory $productRenderExtensionFactory
    ) {
        $this->productRenderExtensionFactory = $productRenderExtensionFactory;
    }

    /**
     * @param ProductInterface $product
     * @param ProductRenderInterface $productRender
     */
    public function collect(ProductInterface $product, ProductRenderInterface $productRender)
    {
        /** @var \Magento\Catalog\Api\Data\ProductRenderExtensionInterface $extensionAttributes */
        $extensionAttributes = $productRender->getExtensionAttributes();

        if (!$extensionAttributes) {
            $extensionAttributes = $this->productRenderExtensionFactory->create();
        }

        $extensionAttributes->setKunjData([
            'Test Kunj',
            '$1000.00'
        ]);
        $productRender->setExtensionAttributes($extensionAttributes);
    }

}

Here is my example screenshot.

enter image description here

4
  • Hi @kunj thanks a lot for the answer +1, I will check this one and get back to you. May 1, 2020 at 11:43
  • 1
    Sure, happy to help, I will learn new thing
    – kunj
    May 1, 2020 at 12:28
  • Hi @kunj I just want to add one more key into into the price data of product_data_storage May 4, 2020 at 15:40
  • 1
    Hi @AdityaShah I have update my code. I have used an extension attribute for extra data.
    – kunj
    May 5, 2020 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.