If a customer purchased a product at the price 100 and then the price of that product has been changed to 200. But whenever the customer visits the website, for them it needs to display the price as same they purchased previous time.
For this requirement, we need to set product prices based on the customer on the website. For this, we have developed a solution on Magento that whenever fetch product price we will check the customer and his previous orders and match whether the product is purchased or not. If purchased then the purchased price will be set to the product.
Through a plugin we can achieve this for that
create di.xml file in Vendor/Module/etc/frontend/di.xml
<type name="Magento\Catalog\Model\Product">
<plugin name="jj_change_product" type="Vendor\Module\Plugin\Model\Product" sortOrder="1" />
</type>
Create plugin file on the specified folder Vendor\Module\Plugin\Model
product.php
class Product
{
public function __construct(
\Magento\Framework\Data\Form\FormKey $formKey,
\Magento\Customer\Model\Session $customerSession,
\Magento\Sales\Model\Order $order
)
{
$this->formKey = $formKey;
$this->customerSession = $customerSession;
$this->_order = $order;
}
public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
{
// Custom code here
}
}
On here can able to set the product price with respect to the require.
This function will be triggered each time a product price call will be triggered. Also over here, we can set manipulating prices.
Thank you.