Search code examples
phpmagentomodelblockobservers

Magento - How do I grab data from observer model


I'm trying to create a simple product selector. The the post data from form was transfer to controller to observer. If I var_dump in observer, the data was present however I can't access it on my block class.

This is my controller:

public function indexAction() {

    $session  = Mage::getSingleton('core/session');

    if ($data = $this->getRequest()->getPost()) {

        $data['ta'] = $this->getRequest()->getPost('torqueAction');
        $data['tr'] = $this->getRequest()->getPost('torqueRequired');
        $data['tm'] = $this->getRequest()->getPost('torqueMetric');

        $eventdata = $data;
        Mage::dispatchEvent('selector_custom_event', $eventdata);
    }
    $this->_redirect('prodselector/index/result');
}

public function resultAction() {
    $this->loadLayout();
    $this->renderLayout();
}

This is my observer

public function observe($observer) {
$event = $observer->getEvent();

$test = $event->getData();

return $test;
}

This is my block:

public function getSelectedProducts() {

$arr_products = array();
$products = Mage::getModel("prodselector/observer")->observe();

return $arr_products;
}

This is how I test on the phtml:

<?php var_dump($this->getSelectedProducts()); ?>

I got an error everytime I execute the module

Fatal error: Call to a member function getEvent() on a non-object in app/code/local/Rts/Prodselector/Model/Observer.php on line 6

PROBLEM:

I can't access the the data from the observer via block class to phtml and I don't understand the error.

QUESTION:

What is the meaning of this error?

How do I access the data from the observer via block class to phtml?

Is my process correct?

Can you give me some guide?

Thanks!


Solution

  • You try to fetch products in the block, but there are no data.

    I recommend the better solution for you:

    Controller:

    public function indexAction() {
    
        $session  = Mage::getSingleton('core/session');
    
        if ($data = $this->getRequest()->getPost()) {
    
            $ta = $this->getRequest()->getPost('torqueAction');
            $tr = $this->getRequest()->getPost('torqueRequired');
            $tm = $this->getRequest()->getPost('torqueMetric');
            $productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('name')
                ->addFieldToFilter('torque_action', $ta)
                ->addFieldToFilter('torque_required', $tr)
                ->addFieldToFilter('torque_metric', $tm);
            Mage::getSingleton('core/session')->setTorqueProductCollection($productCollection);
        }
        $this->_redirect('prodselector/index/result');
    }
    
    public function resultAction() {
        $this->loadLayout();
        $this->renderLayout();
    }
    

    In the block:

    public function getSelectedProducts() {
        $products = Mage::getSingleton('core/session')->getTorqueProductCollection();
        return $products;
    }
    

    In the template:

    <?php foreach ($this->getSelectedProducts() as $product) : ?>
        <?php print_r($product->getName());
    <?php endforeach; ?>
    

    This should not be done by events.