【发布时间】:2016-05-11 23:04:42
【问题描述】:
我正在产品页面view.phtml 内创建一个自定义块。该块创建成功,因为我打开路径提示时可以看到该块。最初我只是为模板使用一些纯 HTML 内容,如下所示:
<div>
this is the new block
</div>
但如果我将模板更改为 phtml 内容,它会返回我正在调用非对象函数的消息。如下:
模板
<?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>
错误信息
致命错误:在非对象上调用成员函数 getName() /home/onebig/public_html/app/design/frontend/ma_hitstore/default/template/catalog/product/view/socialShare.phtml 在第 3 行
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调用块
<?php echo $this->getChildHtml('product.info.socialShare'); ?>
我测试了如果我将模板内容直接粘贴到 view.phtml 中并且它可以工作。我想如果我创建一个自定义块,我必须在某处重新定义所有这些 php 变量 $productName、$productUrl、$productImage?对不起,我是新手。非常感谢您的知识。
【问题讨论】: