How to update the PriceBox cache in Magento 2.

Developers often need to update prices based on customer ID or zipcode. However, when using a plugin to do so in Magento 2, the price may still render from cache based on the customer group. By default, Magento manage the cache based on customer group, so it's necessary to update the price box cache for specific customer IDs or zipcodes. This article will guide you on how to update the price cache for a specific customer ID.

Let's get started!!

If we need to update the cache of the price box in Magento 2, we must update the cache key for the price box with the customer ID. This requires creating a plugin for Magento\Framework\Pricing\Render\PriceBox.

Step 1: Create di.xml file at app/code/Devhooks/ChangePriceBoxKey/etc/frontend/ and paste the below code.

<?xml version="1.0"?> <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\Pricing\Render\PriceBox"> <plugin name="Devhooks_ChangePriceBoxKey_Plugin_Magento_Framework_Pricing_Render_PriceBox" type="Devhooks\ChangePriceBoxKey\Plugin\Block\Pricing\PriceBox" /> </type> </config>

In above code we have defined a plugin for the afterGetCacheKey method. Now we need to create a PriceBox.php file and update the afterGetCacheKey method as per the requirement.

Step 2: Create PriceBox.php file at app/code/Devhooks/ChangePriceBoxKey/Plugin/Block/Pricing/ and paste the below code.

<?php namespace Devhooks\ChangePriceBoxKey\Plugin\Block\Pricing; use Magento\Framework\App\Http\Context; use Magento\Framework\Pricing\Render\PriceBox as PriceBoxAlias; class PriceBox { /** * @var Magento\Framework\App\Http\Context */ private $httpContext; /** * Product constructor. * @param Context $httpContext */ public function __construct( Context $httpContext ) { $this->httpContext = $httpContext; } /** * @param PriceBoxAlias $subject * @param $result * @return string */ public function afterGetCacheKey(PriceBoxAlias $subject, $result) { $customer_id = $this->httpContext->getValue('customer_id'); if ($customer_id) { return implode('-',[$result, $customer_id]); } else { return $result; } } }

As you can see in above code, we've get the customer_id form the httpContext in afterGetCacheKey method and implode it on the current cache key. Due to this changes when customer is login and visit the product page it'll render the pricebox with the different cache key and due to this it'll display the updated price as per your logic. You can use your logic and update the afterGetCacheKey methods as per your need.

If you don't know how to set customer_id in httpContext then please check out another article which is How to set customer data in httpContext and retrieve it in Magento 2..

Now, open Command line in folder root of magento and run the below command.

php bin/magento cache:clean

Bingo!! We are done, now go to the product page and check the price with the different login customer.

In order to update the cache key for the configurable product pricebox, you will need to create plugins using the class provided below in your di.xml files. Use the after plugin for the getCacheKey method and follow the example provided above to make the necessary changes.

<type name="Magento\Swatches\Block\Product\Renderer\Listing\Configurable"> <plugin name="Devhooks_ChangePriceBoxKey_Plugin_Magento_Swatches_Block_Product_Renderer_Listing_Configurable" type="Devhooks\ChangePriceBoxKey\Plugin\Block\Product\Renderer\Listing\Configurable" /> </type> <type name="Magento\Swatches\Block\Product\Renderer\Configurable"> <plugin name="Devhooks_ChangePriceBoxKey_Plugin_Magento_Swatches_Block_Product_Renderer_Configurable" type="Devhooks\ChangePriceBoxKey\Plugin\Block\Product\Renderer\Configurable" /> </type>

I hope this article helped you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.

Review other articles maybe it'll help you too.

Recent Articles
Chrome Extension
Copyright © 2024 All rights reserved.
Ads OFF toggle_off