Why can't I call custom if-check on product view?
-
09-10-2020 - |
문제
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?
해결책
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; ?>