I’ve created a custom attribute in my Magento store in order to show youtube videos by adding the video id in to a text field.
I’ve placed this code in video.phtml which is in catalog/product/view folder
<p>
<?php $attStuff= $_product->getData(); ?>
<?php if( !empty( $attStuff['videoid'] ) ): ?>
<iframe width="330" height="253" src="http://www.youtube.com/embed/<?php echo $attStuff['videoid']; ?>" frameborder="0" allowfullscreen></iframe>
<?php endif; ?>
</p>
I now wish to have the video show in the right sidebar of a CMS page and have added a layout update referencing video.phtml:
<reference name="right">
<block type="core/template" name="catalog.product.video" template="catalog/product/view/video.phtml" />
</reference>
I now get the following error:
Fatal error: Call to a member function getData() on a non-object in /Applications/MAMP/htdocs/mysite/app/design/frontend/bootstrapped/default/template/catalog/product/view/video.phtml on line 13
and I can’t diagnose the problem!
Any ideas as to what might be the problem here?
You are very close. Instead of setting the block type to be
core/template, you need to use a class that has the ability to check the current product. Thecatalog/product_abstractblock has those very functions. Do this for your layout file:Then, in your video file:
Edit:
If you are using it on CMS pages, that becomes a little more challenging. You need to determine what product you want to retrieve the video for. There are a number of ways of doing this. If you are using a layout update in the CMS page, you could do something like this:
And then, in your video file:
I am suggesting this method, because it doesn’t sound as if you are very familiar with creating your own module. I would highly recommend looking that up, because what I am showing you here is not best practice, ie loading a model in a block template file. It should be moved into it’s own block class (which could extend
Mage_Catalog_Block_Product_Abstract, and override thegetProduct()method).Edit #2:
The product ID can be used. In the layout xml, change the
In the template code, use this line: