0

I've got a (custom) module which allows me to add YouTube video's to a product. I call that module in my local.xml like such:

<block type="mymod_catalog/video" name="video" template="catalog/product/view/video.phtml" />

I call the block in my catalog/product/view.phtml like so:

<?php echo $this->getChildHtml('video') ?>

And in the included template file video.phtml it has a check like this:

<?php if($this->getProductVideo()): ?>

But if I use that if-check in my view.phtml it doesn't work (it's always false). I can't figure out why, though. Can anybody tell me what I'm missing?

  • Does video block extend core product block ? – Kishan Patadia May 19 '17 at 13:25
1

It does not work because the getProductVideo method is defined in the block with the alias mymod_catalog/video and inside the template video.phtml, $this is an instance of that block.

In view.phtml, $this is an instance of Magento_Catalog_Block_Product_View and calling the method getProductVideo that does not exist in this block results in calling Magento_Catalog_Block_Product_View::getData('product_video') which I assume will always return null because you never call setProductVideo on this class instance.

You can try to use this in the view.phtml.

<?php $videoBlock = $this->getChild('video');?>
<?php if ($videoBlock && $videoBlock->getProductVideo()) : ?>
    <!-- your code here -->
<?php endif; ?>
  • Marius, hero! Not only did your code work, your easy explanation also helps me grasp the MVVC model in Magento a bit better! Thanks :D – Alex Timmer May 19 '17 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.