1

Is it possible to retrieve data from the observer and then display it on the page through custom block? I'm displaying some products in the home page of Magento through my custom block. I've created a custom observer which listens to the event when the product is clicked. The below code in Observer.php gets the product's data which was clicked on, for example the ID and the Name of the product. When I use Mage::log($pName) for example I can see the clicked product's Name appear in the system.log file. So that's all good. What I want to do next is get the exact same product data from this Observer file and pass it on to my block file so I could use the data in block file. For displaying the product which was clicked on in other block. Right now I have two blocks. One is responsible for all products display and the other one should only show the product which was clicked on. That's why I need to get the data to my block from the Observer.

Observer.php

public function getProductData($observer)
{
    $product = $observer->getProduct();
    $pId = $product->getId();
    $pName = $product->getName();

    //Mage::log($pId);
    //Mage::log($pName);
}

Block file

public function recommended()
{
  //code to retrieve data from observer goes here
  //and then priting the data out 
}

Phtml file

<?php 
$this->recommended();
?>
  • Can you please give some feedback? – sv3n Jul 22 '17 at 2:21
0

I'm not sure want you (exactly) want to achieve ... but in your observer you should be able to set data to registry Mage::register('your_identifier', $data) and get this in your block with $data = Mage::registry('your_identifier').

Maybe it helps to start with ...

Edit:

Observer.php

public function yourMethod(Varien_Event_Observer $observer)
{
    /* @var $product Mage_Catalog_Model_Product */
    $product = $observer->getProduct();
    $product->setYourData($data);
}

In your block (or template file):

$product = Mage::registry('current_product');
$data = $product->getYourData();

Edit 2:

If you don't want to manipulate products data, you need no observer.

public function recommended()
{
    // this gives you the product you currently view
    $product = Mage::registry('current_product');
    return $product->getName();
}

Maybe it's not working, because of missing echo?

<?php echo $this->recommended(); ?>
  • I tried that. And I tried to echo the retrieved data but didn't get nothing. – anthony May 16 '17 at 6:56
  • Which event do you observe? – sv3n May 16 '17 at 10:39
  • I'm observing an event "catalog_controller_product_view". What I want to do is access observer data with custom block and use that block to display the data through phtml. – anthony May 16 '17 at 10:41
  • I assume you want to add/manipulate data for the product? See edited answer ... (Magento1?) – sv3n May 16 '17 at 10:49
  • I edited the question and added my Observer.php file. Right now I can use these variables like $pName and it's value inside this observer file. I need to get these values passed on to my block file so I could use them to display the value of $pName on the page for example. Yes I'm using Magento 1.x. – anthony May 16 '17 at 10:57

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.