0

I am building a plugin which would build the price of product based on formula (param1+param2+param3) with each parameter being store in database.

Files of which are: <MAGENTO-ROOT>/Vendor/Modulename/etc/adminhtml/di.xml

    <?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="cache_price_admin" type="Vendor\Modulename\Plugin\Product" sortOrder="1" />
    </type>
</config>

<MAGENTO-ROOT>/Vendor/Modulename/etc/frontend/di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="cache_price" type="Vendor\Modulename\Plugin\Product" sortOrder="1" />
    </type>
</config>

<MAGENTO-ROOT>/Vendor/Modulename/Plugin/Product.php

<?php
namespace Vendor\Modulename\Plugin;

class Product
{

    public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
    {
        $param1=500; //these values will be dynamically fetched from database
        $param2=600;
        $param3=700;
        return ($param1*0.10)+($param2*0.30)+($param3*0.20);
    }

}

above plugin changes the products prices and displays it on the frontend correctly but price gets cached once products are loaded and doesn't change when changing the formula parameters.

i have tried another approch in which i created the module which interated through all products and updated the price of each with the value from above formula whenever the formula parameters were updated by admin user. but it took more than 20 mins just to update prices of 50 products with re-indexing set to manual mode.And since Number of Products are in the range of 20k this can not work.

so my questions are:

  1. how can i make this plugin work to show updated product prices on frontend and backend as well as in orders whenever a user buys one of these products?

  2. is there any way to make product price update faster when using \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory class and

    $product->setPrice(($param10.10)+($param20.30)+($param3*0.20))->save()

  3. is there any alternative approach to achieve this functionality?

  • One solution could be using an Async reindexing process – Alan Zavagli Aug 6 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.