user1704524
user1704524

Reputation: 458

Adding YouTube video to product and CMS pages Magento

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?

Upvotes: 1

Views: 8339

Answers (1)

Joseph at SwiftOtter
Joseph at SwiftOtter

Reputation: 4321

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. The catalog/product_abstract block has those very functions. Do this for your layout file:

<reference name="right"> 
    <block type="catalog/product_view" name="catalog.product.video" template="catalog/product/view/video.phtml" /> 
</reference>

Then, in your video file:

<p>
    <?php $_product = $this->getProduct(); ?>
    <?php if($_product->getVideoid()): ?>
    <iframe width="330" height="253" src="http://www.youtube.com/embed/<?php echo $_product->getVideoid(); ?>" frameborder="0" allowfullscreen></iframe>
    <?php endif; ?>
</p>

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:

<reference name="right"> 
    <block type="catalog/product_view" name="catalog.product.video" template="catalog/product/view/video.phtml">
        <action method="setProductSku"><sku>sku-here</sku></action>
    </block> 
</reference>

And then, in your video file:

<?php $_product = Mage::getModel('catalog/product')->load($this->getProductSku(), 'sku'); ?>
<?php if ($_product->getId() && $_product->getVideoid()): ?>
<p>
    <iframe width="330" height="253" src="http://www.youtube.com/embed/<?php echo $_product->getVideoid(); ?>" frameborder="0" allowfullscreen></iframe>
</p>
<?php endif;?>

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 the getProduct() method).


Edit #2:

The product ID can be used. In the layout xml, change the

<block type="core/template" name="catalog.product.video" template="catalog/product/view/video.phtml">
    <action method="setProductId"><id>id-here</id></action>
</block>

In the template code, use this line:

<?php $_product = Mage::getModel('catalog/product')->load($this->getProductId()); ?>

Upvotes: 1

Related Questions