2

I want to create observer ,when a product is clicked ,can you tell me which is the event and how can I see in my observer code which product was clicked and to get information about it ?

This is my config.xml :

<?xml version="1.0" encoding="UTF-8"?>
<config>
        <modules>
        <Solvingmagento_OrderExport>
            <version>0.1.0</version>
        </Solvingmagento_OrderExport>
        </modules>
        <global>
        <events>
            <catalog_controller_product_view>
            <observers>
                <custom_catalog_controller_product_view>
                    <class>solvingmagento_orderexport/observer</class>
                    <method>observerProductClicked</method>
                </custom_catalog_controller_product_view>
            </observers>
        </catalog_controller_product_view>
        </events>

        <helpers>
            <orderexport>
            <class>Solvingmagento_OrderExport_Helper</class>
            </orderexport>
        </helpers>
        </global>
    </config>

and my Observer.php :

class SolvingMagento_OrderExport_Model_Observer{

public function observerProductClicked(Varien_Event_Observer $observer)
{
    $product = $observer->getProduct();
    // Do what you want with the product
    echo $product;
    var_dump($_SERVER);
    die('chekc');
}

}

1 Answer 1

1

To observe a product clicked/viewed you need to use the following event: catalog_controller_product_view

In your config.xml add the following to your <global> tag:

    <events>
        <catalog_controller_product_view>
            <observers>
                <custom_catalog_controller_product_view>
                    <class>yourmodule/observer</class>
                    <method>observeProduct</method>
                </custom_catalog_controller_product_view>
            </observers>
        </catalog_controller_product_view>
    <events>

NB: you will need to replace module/observer with your module class name.

Then in your Observer.php you can do:

<?php

class Vendor_Module_Model_Observer {
    public function observerProductClicked(Varien_Event_Observer $observer)
    {
        $product = $observer->getProduct();
        // Do what you want with the product

    }
}
11
  • Thank you ,but its not working ,my data is imported from magento sample and when I click on some product nothing is happening. This is my config.xml :
    – dobrganch
    Commented Apr 26, 2016 at 11:02
  • <events> <catalog_controller_product_view> <observers> <custom_catalog_controller_product_view> <class>solvingmagento_orderexport/observer</class> <method>observerProductClicked</method> </custom_catalog_controller_product_view> </observers> </catalog_controller_product_view> </events>
    – dobrganch
    Commented Apr 26, 2016 at 11:02
  • @dobrganch please post your config.xml in your question it'll be easier to read Commented Apr 26, 2016 at 11:02
  • Done ,my data is sample from magento isntallation ,I am clicking on products but nothing happen
    – dobrganch
    Commented Apr 26, 2016 at 11:04
  • @dobrganch you haven't declared your model class, please add the following to your <global> tag : <models><solvingmagento_orderexport><class>Solvingmagento_OrderExport_Model</class></solvingmagento_orderexport></models> Commented Apr 26, 2016 at 11:07

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.