简体   繁体   English

magento phtml内容未显示在产品页面view.phtml中

[英]magento phtml content does not show in product page view.phtml

I am creating a custom block inside product page view.phtml . 我正在产品页面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: 最初,我只是为模板使用一些简单的HTML内容,如下所示:

<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. 但是,如果我将模板更改为phtml内容,它将返回消息,说明我正在调用非对象函数。 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 致命错误:在第3行的/home/onebig/public_html/app/design/frontend/ma_hitstore/default/template/catalog/product/view/socialShare.phtml中的非对象上调用成员函数getName()

catalog.xml I have the following codes catalog.xml我有以下代码

<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 view.phtml调用该块

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

I tested If I paste the template content directly in view.phtml and it works. 我测试了是否将模板内容直接粘贴到view.phtml中,并且可以正常工作。 I guess If I create a custom block I have to redefine all theses php variable $productName , $productUrl , $productImage somewhere? 我想如果我创建一个自定义的块我必须重新定义所有论文PHP变量$productName$productUrl$productImage地方? Sorry I am new to this. 抱歉,我是新来的。 Your knowledge is greatly appreciated. 非常感谢您的知识。

This issue is that your child block has the type core/template . 问题是您的子块的类型为core/template When you call getProduct on a core template it is just going to return null. 当您在核心模板上调用getProduct ,它将仅返回null。

If you update it to catalog/product_view then $this->getProduct() will return the product. 如果将其更新为catalog/product_view$this->getProduct()将返回产品。


Alternatively, you can also get the current product from any template by fetching it from the registry: 或者,您也可以通过从注册表中获取任何模板来从其获取当前产品:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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