Fisher
Fisher

Reputation: 1794

Setting model as template data (attribute) for child block

In my product view template i'm loading child template, and transfering product instance to be available in this child template:

<?php
    echo $this->getLayout()
        ->createBlock('core/template')
        ->setTemplate('catalog/product/view/addedToCartDialog.phtml')
        ->setAttribute('product', $_product)
        ->toHtml();
?>

Then in my catalog/product/view/addedToCartDialog.phtml i'm trying to use this product instance:

<?php $product = $this->getData('product'); ?>
<?php echo"<pre>";print_r($product->getId());echo"</pre>"; ?>

However it seems to be not loaded: Fatal error: Call to a member function getId() on a non-object in /home/ryba/workspace/polcode/Greenlights/app/design/frontend/default/greenlights/template/catalog/product/view/addedToCartDialog.phtml on line 2 But when i check variable $product with print_r:

<?php echo"<pre>";print_r($product);echo"</pre>"; ?>

It is displayed that this variable is correct Mage_Catalog_Model_Product Object, also checked if attributes is correct (like sku, name etc) - everything is correct.

What is wrong with this ?

Upvotes: 0

Views: 2818

Answers (1)

Mike Potter
Mike Potter

Reputation: 328

I'm going to give you several answers. The first is the direct answer to your question. The rest are alternatives, but better ways to do what you're trying. The last answer is, in my opinion, the best.

Direct Answer:

Instead of using setAttribute, just use the magic setter/getter methods:

<?php
// In catalog/product/view.phtml
echo $this->getLayout()
    ->createBlock('core/template')
    ->setTemplate('catalog/product/view/addedToCartDialog.phtml')
    ->setProduct($_product)
    ->toHtml();
?>

<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>

Better:

And, if you know you are in a template loaded by the catalog/product controller, you can get the product this way.

<?php
// In catalog/product/view.phtml
echo $this->getLayout()
    ->createBlock('core/template')
    ->setTemplate('catalog/product/view/addedToCartDialog.phtml')
    ->toHtml();
?>

<?php
// In addedToCartDialog.phtml
$_product = Mage::registry('product');
echo $_product->getId();
?>

Even Better

The best way would be to use a different block type which has the methods already loaded (again, if you know you are in a template loaded by the catalog/product controller)

<?php
// In catalog/product/view.phtml
echo $this->getLayout()
    ->createBlock('catalog/product_view')
    ->setTemplate('catalog/product/view/addedToCartDialog.phtml')
    ->toHtml();
?>

<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>

And Finally, the Best

One last item of business. The better way to add extra blocks to your templates is to add the block in your local.xml file.

<!-- Local.xml -->
<catalog_product_view translate="label">
  <reference name="content">
    <block type="catalog/product_view" name="addedToCartDialog" as="addedToCartDialog" template="catalog/product/view/addedToCartDialog.phtml" />
  </reference>
</catalog_product_view>

Now, set up your phtml file

<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>

Then call the block from your phtml file

// In catalog/product/view.phtml
<?php echo $this->getChildHtml('addedToCartDialog'); ?>

Upvotes: 1

Related Questions