1
votes

I have two custom blocks in my homepage of Magento. One of the blocks displays all the products. And the other block should display the product which was recently clicked on/viewed. I have created the observer for the event to get the clicked product data. How can I use this data from observer to show that product's data in my second block in the homepage? I have to pass the data from the observer somehow.

Observer.php

<?php
include 'C:\wamp64\www\magento1924\app\code\local\Company\Namespace\Block\Recommended.php';

class Company_Namespace_Model_Observer {

public function getProductData($observer) {
    //Uncomment the line below to log what is contained here:
    //Mage::log($observer);

    $data = new Company_Namespace_Block_Recommended();
    $product = $observer->getProduct();
    $pId = $product->getId();
    $pName = $product->getName();


    //Mage::log($pId);
    Mage::log($pName);
    $data->recommended($pId, $pName);

}
}
?>

Block file Recommended.php

<?php
class Company_Namespace_Block_Recommended extends Mage_Core_Block_Template 
{    
public function recommended($pId, $pName){
    echo $pId;
    echo $pName;
    echo "Test!";
}
}
?>

Recommended.phtml

<h1>Recently viewed</h1>
<?php
    $this->recommended();
?>

So I'm using this function recommended() to print out the clicked product's data. When I'm in the homepage and I click something, it takes me to the product view page in which I can also see (in the upper part) the product's name and id and also "Test!"(which was also echoed out in the function). What I want to have is that since I'm also callin the same function recommended() out in the homepage (in the phtml file) I should see the products id, name and also "Test!". The problem is it is only showing the "Test!". Am I doing something totally wrong?

2
What have you tried so far? Please have a look at stackoverflow.com/help/how-to-askPVitt
I've edited the question.anthony

2 Answers

0
votes

I will suggested you to use cookies. Once a person clicks on a Product, save their IDs in cookie. And in the block class read the cookie values and grab the product collection like this:

$productIds = array('384','385','385'); // Dummy ids, fetch them from cookies

$products = Mage::getModel('catalog/product')
 ->getCollection()
 ->addAttributeToSelect('*') 
 ->addAttributeToFilter('status', 1)
 ->addAttributeToFilter('visibility', 4)
 ->addIdFilter($productIds);
 ; 

I hope this will give you an idea.

0
votes

Please follow below step to get recently clicked/viewed on home page:

1- Click on CMS->Widgets

2- Click on the 'Add new widget instance'

3- Select Type->Recently Viewed Products

4- Select Design Package Theme->Your Theme

5- Click on Continue Button

6- In Frontend Properties

a. Give the Widget Instance Title

b. Assign to store views->All Store Views

c. Scroll a little down and you will notice a 'Layout updates' section

i. Click on Add Layout Updates

ii. Display On -> Specified Page (you can select any other option too)

  1. Page->(Select your desired page)

  2. Block Reference->(Select your reference to show it)

7- In Widget Options

a. Set 'Number of Products to display'

8- Click on 'Save' button and check your specified page. Don't forget to clear the cache if you don't see anything.