1

I'm trying to develop a module to create a log for each product visited by a user. To generate a log, I'm using event observer. But the event never is called.

What could be wrong? My code: C:\Users\miudo\AppData\Local\Temp\scp44107\home\sr\localhos.t\app\code\local\Bit\Itemsviewed\Model\Observer.php

<?php
class Bit_Itemsviewed_Model_Observer 
{
    public function saveProductVisitHistory(Varien_Event_Observer $observer) {
        $event = $observer->getEvent();

        Mage::log('id ' . $observer->getEvent()->getProduct()->getId() , null, 'custom.log');

        return $this;
    }
}

C:\Users\miudo\AppData\Local\Temp\scp47708\home\sr\localhos.t\app\code\local\Bit\Itemsviewed\etc\Config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Bit_Itemsviewed>
            <version>0.1.0</version>
        </Bit_Itemsviewed>
    </modules>
    <global>
        <helpers>
          <itemsviewed>
            <class>Bit_Itemsviewed_Helper</class>
          </itemsviewed>
        </helpers>
        <blocks>
          <itemsviewed>
            <class>Bit_Itemsviewed_Block</class>
          </itemsviewed>
        </blocks>       
        <catalog>
            <rewrite>
                <product_view>Bit_Itemsviewed_Block_Itemsviewed</product_view>
            </rewrite>
        </catalog>
        <models>
            <itemsviewed>
                <class>Bit_Itemsviewed_Model</class>        
            </itemsviewed>
        </models>
    <!--/global>
    <frontend-->
        <events>
            <catalog_controller_product_view>
                <observers>
                    <itemsviewed>
                        <type>model</type>
                        <class>bit_itemsviewed/observer</class>
                        <method>saveProductVisitHistory</method>
                    </itemsviewed>
                </observers>
            </catalog_controller_product_view>
        </events>
    </global>
</config>

C:\Users\miudo\AppData\Local\Temp\scp57676\home\sr\localhos.t\app\code\local\Bit\Itemsviewed\Helper\Data.php

<?php
class Bit_Itemsviewed_Helper_Data extends Mage_Core_Helper_Abstract{
}

C:\Users\miudo\AppData\Local\Temp\scp48515\home\sr\localhos.t\app\code\local\Bit\Itemsviewed\Block\Itemsviewed.php

<?php
class Bit_Itemsviewed_Block_Itemsviewed extends Mage_Catalog_Block_Product_View{
}

Thanks

EDITED: I had been trying to use

<itemsviewed>
  <type>model</type> also <singleton>
  <class>itemsviewed/observer</class> also <class> Bit_Itemsviewed_Model_Observer </class>
 [...]
</itemsviewed>
2

You are declaring your models under the shortname/namespace itemsviewed, but when you declare your event observer you are using bit_itemsviewed. It should therefore be:

<class>itemsviewed/observer</class>
| improve this answer | |
  • Already try your suggestion.. but not worked... – D Miller Jan 15 '15 at 0:43
  • Ah.. and thanks Amit and Cags, now I understand the syntax needed in events xml structure. – D Miller Jan 15 '15 at 12:22
1

D Millar try this event

catalog_controller_product_init_after instead of catalog_controller_product_view event .

Mage::dispatchEvent('catalog_controller_product_init_after',
    array('product' => $product,
        'controller_action' => $controller
    )

for Observer code is look like:

<?php
class Bit_Itemsviewed_Model_Observer 
{
    public function saveProductVisitHistory(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $product=$event->getProduct()
        $controller_action=$event->getControllerAction()
        Mage::log('id ' . $observer->getEvent()->getProduct()->getId() , null, 'custom.log');

        return $this;
    }
}

Also as Cags said you need to change you class of observer from bit_itemsviewed/observer to itemsviewd as Your model identifier is <itemsviewed> because of

 <models>
    <itemsviewed>  <!-- model indentifier -->
        <class>Bit_Itemsviewed_Model</class>        
    </itemsviewed>
</models>
| improve this answer | |
  • Still not working. But I'm beginning to think that my solution will not work. I believe, that when a product is visited, this log should be created in a block, and not using an observer. It will be easier. – D Miller Jan 15 '15 at 12:21
0

Use this example to make it work, as I just tested it:

<frontend>
<events>
   <catalog_controller_product_init_after>
            <observers>
                <gtm_see_product_details>
                    <class>gtm/observer</class>
                    <method>setGTMSeeProductDetails</method>
                </gtm_see_product_details>
            </observers>
        </catalog_controller_product_init_after>
...

path: app\code\local\OsfMinitrade\GTM\etc\config.xml

/**
 * @param Varien_Event_Observer $observer
 * @return Varien_Event_Observer
 */
public function setGTMSeeProductDetails(Varien_Event_Observer $observer)
{
    var_dump($observer->getEvent()->getProduct()->getId());
    die;

    return $observer;
}

path: app\code\local\OsfMinitrade\GTM\Model\Observer.php

| improve this answer | |

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.