0

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()
 ->createBlock('easylife_switcher/catalog_product_view_type_configurable_config')
 ->setTemplate('easylife_switcher/catalog/product/view/type/configurable/config.phtml')
 ->setProduct($_product)->toHtml();

Below is config.phtml file code.

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

Below is the getJsonAdditionalConfig() function code:

public function getJsonAdditionalConfig()
{
    var_dump($this->getProduct()->getId());exit;
}

Right now it returns

Call to a member function getId() on null in

How can I pass $_product instance to easylife_switcher block.

Thanks

4
  • 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
    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 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
    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 Aug 23, 2016 at 12:06

2 Answers 2

0

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

remove

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

This will put $product on "registry", like

app/code/core/Mage/Catalog/Block/Product/View.php

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

0

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();
    if($this->getData('product_id')){
        $model->load($this->getData('product_id'));
    }
    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(){
        if(!$this->product){
             if($this->getData('product')){
                 $this->product = $this->getData('product');
             }
        }
        if(!$this->product){
             $model = Mage::getModel();
             if($this->getData('product_id')){
                 $model->load($this->getData('product_id'));
             }
             $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.