2

below is my config.xml file code.

<?xml version="1.0"?>
<config>
    <modules>
        <Eventprice_Meta>
            <version>0.0.1</version>
        </Eventprice_Meta>
    </modules>
    <global>
        <models>
            <eventprice_meta>
                <class>Eventprice_Meta_Model_Observer</class>
            </eventprice_meta>
        </models>
    </global>
    <adminhtml>
        <events>
            <catalog_product_get_final_price><!-- observe the event -->
                <observers>
                    <eventprice_meta>
                        <type>singleton</type>
                        <class>Eventprice_Meta_Model_Observer</class>
                        <method>changeprice</method>
                    </eventprice_meta>
                </observers>
            </catalog_product_get_final_price>
        </events>
    </adminhtml>
</config>

below is my Observer.php files code.

<?php 
class Eventprice_Meta_Model_Observer {

    public function changeprice(Varien_Event_Observer $event) {
        return 5;
        if($_GET['id']){
            $pricetoadd=Mage::getModel('catalog/product')
                        ->load($_GET['id'])->getPrice();

            $product = $event->getEvent()->getProduct();
            $originalprice = $product->getPrice();
            $customprice = $originalprice+$pricetoadd;
            $product->setPrice($customprice);
        }
        echo 'call';
        exit;
   }
}

i want to set the Product price $5 on product view page. i write a function 'changeprice' for changing the product price. but this function is not call please checked where i create a mistake in code.

8

You declared your observer in the <adminhtml> area. So it does not get triggered on frontend.
You need to replace

<adminhtml>
    <events>
    ....

with

<frontend>
    <events>
    ....

Next problem. return 5; will not make the price of the product 5. It will do nothing.
You need to make it $observer->getProduct()->setFinalPrice(...your price here...)

Next one. Don't use $_GET['id']. Use Mage::app()->getRequest()->getParam('id')

And the last one for now. calling return 5 at the top of the method will not execute the rest of the method. So you never reach echo 'call'; exit;

  • Perfect solution.+1 for me. – liyakat May 26 '15 at 12:20
  • right Thanks awesome thanks this is working. – Renish Khunt May 26 '15 at 12:24
1

Observer parameters issue:

There are issue with parameters on observer. For getting the product id try with $observer->getProduct()->Id(); As system when catalog_product_get_final_price event fire then the two parameter has been sended.

  • Current product Object: $event->getEvent()->getProduct();
  • Other Qty: $event->getEvent()->getQty().

No need load product object again.

Mage::dispatchEvent('catalog_product_get_final_price', array('product' => $product, 'qty' => $qty));

As @Marius told for set the final price,you need to use $event->getEvent()->getProduct()->setFinalPrice() for set final price and set event area are from adminhtml to frontend for properly works.

Your Answer

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

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