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();
?>
0

2 Answers 2

1

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(); ?>
13
  • I tried that. And I tried to echo the retrieved data but didn't get nothing.
    – anthony
    May 16, 2017 at 6:56
  • Which event do you observe?
    – sv3n
    May 16, 2017 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, 2017 at 10:41
  • I assume you want to add/manipulate data for the product? See edited answer ... (Magento1?)
    – sv3n
    May 16, 2017 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, 2017 at 10:57
0

Depending on how you are instantiating your block you could also get the block in your observer and use the _data methods of getting/setting its data. If you are calling createBlock in a template or block this will not work, but if you are defining your block in layout this should work just fine.

public function getProductData($observer)
{
    $product = $observer->getProduct();
    $pId = $product->getId();
    $pName = $product->getName();
    $block = Mage::app()->getLayout()->getBlock('<your_block_name_here>');
    if($block)
    {
        $block->setProductId($pId);
        $block->setProductName($pName);
    }

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

Then in your block:

public function recommended()
{
    $pId = $this->getProductId();
    $pName = $this->getProductName();
}

This works because the layout object caches each block it creates in a _blocks property which is keyed by the block name, so when it goes to generate the block from toHtml it will get the same instance you just set those properties on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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