Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7161869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:38:24+00:00 2026-05-28T13:38:24+00:00

In my product view template i’m loading child template, and transfering product instance to

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:38:25+00:00Added an answer on May 28, 2026 at 1:38 pm

    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'); ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Putting this line to view.phtml ... <?php echo $this->getChildHtml('upsell_products') ?> ... raises this error:
I am writing a module to extend the Core/Catalog/Product/View/Media.php class so I can expose
how can i add a new price template to the category view (template/catalog/product/list.phtml) without
In the file $MAGENTO_PATHapp/design/frontend/base/default/template/catalog/product/view/options/wrapper.phtml , I see the following line of code: <?php echo
I try to create a product detail (accommodation details) view based on this controller
I would like to add 3 links to random products on the .../template/catalog/product/view.phtml template.
I'm trying to modyfiy magento's product page layout ( app/design/frontend/default/*template*/template/catalog/product/view.phtml ) but i'm not
Can i print out a url /admin/manage/products/add of a certain view in a template?
I'm customizing the product view page and I need to show the user's name.
I currently have a product view page that contains an MVCContrib HTML Grid with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.