I want to create dynamic pricing for the products on the Magento 2 store. After much reading on the internet I understand this can be achieved by:

  1. Create a custom module
  2. Register the event and observer in events.xml
  3. Update the product price in Observer file

I did all of these, however I'm not able to trigger the observer. Can someone please let me know what could be wrong here?

Relevant file contents:

  • app/code/Vendor/ModuleName/etc/events.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="catalog_product_get_final_price">
                <observer instance="Vendor\ModuleName\Observer\Catalog\ProductGetFinalPrice" name="dynamicprice"/>
        <event name="catalog_product_prepare_index_select">
                <observer instance="Vendor\ModuleName\Observer\Catalog\ProductGetFinalPrice" name="dynamicprice"/>
  • app/code/Vendor/ModuleName/Observer/Catalog/ProductGetFinalPrice.php
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.

namespace Vendor\ModuleName\Observer\Catalog;

class ProductGetFinalPrice implements \Magento\Framework\Event\ObserverInterface

     * Execute observer
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
            //Your observer code
           echo "Hello";
        $writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/testlog.log');
    $logger = new \Laminas\Log\Logger();
    $logger->info("Final Price Observer called..... ");
    $product = $observer->getProduct();
    $logger->info("Prodcut: ".$product);

I have the registation.php and module.xml files in the module and it is successfully registered. However, I cannot catch any event in testlog.log file.

I have added a simple product enter image description here

I expect to refresh this page and the event should trigger.

I event tried moving events.xml to frontend directory but still no good. I don't know how to debug this now. Need help.

1 Answer 1


You can create your own observer or debug the existing one i.e there are two similar events triggers from different classes, make sure you are using the correct one.

Event name: catalog_product_get_final_price File: vendor/magento/module-catalog/Model/Product/Type/Price.php

Event name: catalog_product_get_final_price File: vendor/magento/module-bundle/Model/Product/Price.php

  • I created my own observer(pasted above in question). I am still not receiving any events there. I don't see any logs. I want to override getFinalPrice() method. Commented Jan 16, 2021 at 16:56

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.