【发布时间】:2014-07-09 13:30:38
【问题描述】:
如何获取评论的 list.phtml 上的评论计数?
frontend\base\default\template\review/product/view/list.phtml
当我在此页面中使用此代码时出现错误,
<?php
// Get the number of reviews of the product on the product page.
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')
->setStoreId($storeId)
->load($_product->getId());
/* @var $summaryData Mage_Review_Model_Review_Summary */
/*
array(
['primary_id'] => 147
['entity_pk_value'] => 166
['entity_type'] => 1
['reviews_count'] => 1
['rating_summary'] => 80
['store_id'] => 1
)
*/
//var_dump($summaryData['reviews_count']);
?>
错误,
Fatal error: Call to a member function getId() on a non-object in
虽然frontend\base\default\template\review/product/view/count.phtml 的计数将始终返回empty
<?php if (!empty($count)):?>
<a href="#customer-reviews" class="nobr"><?php echo $this->__('%s Review(s)', $count) ?></a>
<?php endif;?>
知道为什么吗?
我的 catalog.xml 的 <catalog_product_view translate="label"> 中有代码,
<!-- adding review to the product page -->
<block type="page/html_pager" name="product_review_list.toolbar">
<action method="setLimit"><limit>2</limit></action>
</block>
<block type="review/product_view_list" name="product.info.product_additional_data" as="product_review" template="review/product/view/list.phtml">
<block type="core/template" name="product_review_list.count" template="review/product/view/count.phtml" />
<block type="review/form" name="product.review.form" as="review_form"/>
</block>
<!-- adding review to the product page -->
【问题讨论】:
-
$summaryData->getReviewsCount() 或 $summaryData->getData('reviews_count') 有效吗?
-
另外,你从哪里得到 $_product->getId() 因为我没有在你的代码中看到产品模型? “getId()”错误是因为没有产品型号。试试这个,把这段代码放在 "$_storeID = ..." $_product = Mage::registry('current_product'); 之前或之下
-
我应该这样做以获得评论计数
<?php $_helper = $this->helper('catalog/output'); ?> <?php $_product = $this->getProduct(); ?> <?php $storeId = Mage::app()->getStore()->getId(); $summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)->load($_product->getId()); //var_dump($summaryData['reviews_count']); ?> -
感谢您的回答和提示! :D
标签: php magento magento-1.8