I want to pass $_product instance to createBlock, I have done below code but it does not working. I have written this code on product listing page list.phtml file.

echo Mage::app()->getLayout()

Below is config.phtml file code.

<?php if (Mage::helper('easylife_switcher')->isEnabled()) : ?>
<script type="text/javascript">
    var switcherConfig = <?php echo $this->getJsonAdditionalConfig() ?>;
    document.observe('dom:loaded', function(){
        if (spConfig){
            spConfig = new Easylife.Switcher(Object.extend(spConfig.config, switcherConfig));

Below is the getJsonAdditionalConfig() function code:

public function getJsonAdditionalConfig()

Right now it returns

Call to a member function getId() on null in

How can I pass $_product instance to easylife_switcher block.


  • try to echo the Product Id before echo Mage::app() [...]. If you get the same error, you know your $_product Variable hasn't a valid Product Model. If you get the ID try to figure out what class $_product is. You can test it with something like echo get_class($this->getProduct()); in the getJsonAdditionalConfig() method.
    – Pawel
    Commented Aug 23, 2016 at 11:35
  • I print the product id before Mage::app()[...] using echo $_product->getId() it's working return product id. and also i print the class using echo get_class($this->getProduct()); the output is Easylife_Switcher_Block_Catalog_Product_View_Type_Configurable_Config it's return class name Commented Aug 23, 2016 at 11:40
  • do you tried to echo the class inside the getJsonAdditionalConfig() method? Try this inside of getJsonAdditionalConfig(): var_dump($this->getProduct()->debug());` Do you see any Product data? Your next step could be to pass other data to your Template like: ->setFoo('bar')->toHtml(); and see if you can echo it. I think the best way to debug it with xdebug.
    – Pawel
    Commented Aug 23, 2016 at 11:59
  • Yes i print class name inside the getJsonAdditionalConfig() function. and i tried debug() it's return Call to a member function debug() on null Commented Aug 23, 2016 at 12:06

2 Answers 2


After some time I've found a possible solution, try to add

Mage::register('product', $product)
Mage::register('current_product', $product) // used for "qtyInstallments"

Before the

echo Mage::app()->getLayout()->createBlock()[..] and


->setProduct($product) from it (I've tried this setProduct() too, without success).

This will put $product on "registry", like


getProduct() method (around line 89) is expecting :D


Not Right: $block->setProduct($_product);

Right: $block->setData('product', $_product);

Or: $block->addData(['product'=>$_product]);

And Get: $this->getData('product');

Also you can try: pass only product_id

$block->setData('product_id', $_product->getId());

And after:

private function getProduct(){
    $model = Mage::getModel();
    return $model->getId();

Or Combine Methods:

class ... {
    private $product;
    public function setProduct(Mage_Catalog_Model_Product $_product){
        $this->product = $_product;
        return $this;
    private function getProduct(){
                 $this->product = $this->getData('product');
             $model = Mage::getModel();
             $this->product = $model;
        return $this->product;

Your Answer

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

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