2014-07-09 41 views
0

如何在評論的list.phtml中獲得評論數?如何在Magento 1.8中查看評論的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計數總是會返回

<?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 --> 
+1

$ summaryData-> getReviewsCount()或$ summaryData-> getData('reviews_count')是否工作? – aki

+1

另外,您在哪裏獲得$ _product-> getId(),因爲我在代碼中看不到產品模型? 「getId()」錯誤是因爲沒有產品模型。試試這個,把這個代碼放在「$ _storeID = ...」之前或之下。$ _product = Mage :: registry('current_product'); – aki

+0

我應該這樣做得到審查計數'<?php $ _helper = $ this-> helper('catalog/output'); ?> <?php $ _product = $ this-> getProduct(); ?> <?php $ storeId = Mage :: app() - > getStore() - > getId(); ($ storeId) - > load($ _ product-> getId()); $ summaryData = Mage :: getModel('review/review_summary') - > setStoreId($ storeId) // var_dump($ summaryData ['reviews_count']); ?>' – laukok

回答

3

請嘗試非常簡單的代碼這是爲我工作的代碼。

?php echo $this->getReviewsSummaryHtml($_product, 'short')?> 

這看起來像附加的截圖。 enter image description here