Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. </p> <p><strong>Direct Answer:</strong></p> <p>Instead of using setAttribute, just use the magic setter/getter methods:</p> <pre><code>&lt;?php // In catalog/product/view.phtml echo $this-&gt;getLayout() -&gt;createBlock('core/template') -&gt;setTemplate('catalog/product/view/addedToCartDialog.phtml') -&gt;setProduct($_product) -&gt;toHtml(); ?&gt; &lt;?php // In addedToCartDialog.phtml $_product = $this-&gt;getProduct(); echo $_product-&gt;getId(); ?&gt; </code></pre> <p><strong>Better:</strong></p> <p>And, if you know you are in a template loaded by the catalog/product controller, you can get the product this way.</p> <pre><code>&lt;?php // In catalog/product/view.phtml echo $this-&gt;getLayout() -&gt;createBlock('core/template') -&gt;setTemplate('catalog/product/view/addedToCartDialog.phtml') -&gt;toHtml(); ?&gt; &lt;?php // In addedToCartDialog.phtml $_product = Mage::registry('product'); echo $_product-&gt;getId(); ?&gt; </code></pre> <p><strong>Even Better</strong></p> <p>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)</p> <pre><code>&lt;?php // In catalog/product/view.phtml echo $this-&gt;getLayout() -&gt;createBlock('catalog/product_view') -&gt;setTemplate('catalog/product/view/addedToCartDialog.phtml') -&gt;toHtml(); ?&gt; &lt;?php // In addedToCartDialog.phtml $_product = $this-&gt;getProduct(); echo $_product-&gt;getId(); ?&gt; </code></pre> <p><strong>And Finally, the Best</strong></p> <p>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.</p> <pre><code>&lt;!-- Local.xml --&gt; &lt;catalog_product_view translate="label"&gt; &lt;reference name="content"&gt; &lt;block type="catalog/product_view" name="addedToCartDialog" as="addedToCartDialog" template="catalog/product/view/addedToCartDialog.phtml" /&gt; &lt;/reference&gt; &lt;/catalog_product_view&gt; </code></pre> <p>Now, set up your phtml file</p> <pre><code>&lt;?php // In addedToCartDialog.phtml $_product = $this-&gt;getProduct(); echo $_product-&gt;getId(); ?&gt; </code></pre> <p>Then call the block from your phtml file</p> <pre><code>// In catalog/product/view.phtml &lt;?php echo $this-&gt;getChildHtml('addedToCartDialog'); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload