简体   繁体   中英

magento phtml content does not show in product page view.phtml

I am creating a custom block inside product page view.phtml . The block is successfully created because I can see the block when I turn on the path hint. Initially I was just using some plain HTML content for the template something like below:

<div> 
this is the new block
</div>

But if I change the template to phtml content, it returns message that I am calling a non-object function. like below:

TEMPLATE

<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<?php $productName = $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
<?php $productUrl = $_helper->productAttribute($_product, $_product->getProductUrl(), 'product_url'); ?>
<?php $productImage = $_product->getImageUrl() ?>  

<div class="socialShare clearfix">
    <ul>
        <li><a class="fa fa-facebook" href="javascript:popWin('https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode($productUrl); ?>&t=<?php echo urlencode($productName); ?>', 'facebook', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');" title="<?php echo $this->__('Share it') ?>"></a></li>
    </ul>
</div>

ERROR MESSAGE

Fatal error: Call to a member function getName() on a non-object in /home/onebig/public_html/app/design/frontend/ma_hitstore/default/template/catalog/product/view/socialShare.phtml on line 3

catalog.xml I have the following codes

<catalog_product_view translate="label">
    <reference name="content">
        <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
            <block type="core/template" name="product.info.socialShare" template="catalog/product/view/socialShare.phtml"/>
    </reference>
 </catalog_product_view>

view.phtml call the block

<?php echo $this->getChildHtml('product.info.socialShare'); ?>

I tested If I paste the template content directly in view.phtml and it works. I guess If I create a custom block I have to redefine all theses php variable $productName , $productUrl , $productImage somewhere? Sorry I am new to this. Your knowledge is greatly appreciated.

This issue is that your child block has the type core/template . When you call getProduct on a core template it is just going to return null.

If you update it to catalog/product_view then $this->getProduct() will return the product.


Alternatively, you can also get the current product from any template by fetching it from the registry:

$_product = Mage::registry('current_product');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM