Magento使用星级评论Star Rating 【magento二次开发】

来源:互联网 发布:广州达内java培训 编辑:程序博客网 时间:2024/05/17 22:50
在Magento产品页,用户可以对产品进行评级,Magento默认使用Radio让用户选择评分等级,用户体验不是很好,
基于jQuery星级评分插件有很多,这里我们使用jQuery Star Rating Plugin这款插件。

1 加载jQuery
首先需要在Magento中需要加载jQuery,下载StarRating插件并放在网站根目录的js目录下,比如/js/star_rating

2 新建模块
在/app/code/local/MagentoBoy/StarRating目录下新建一个模块MagentoBoy_StarRating,并添加模块文件:
/app/etc/modules/MagentoBoy_StarRating.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MagentoBoy_StarRating>
  5.             <active>true</active>
  6.             <codePool>local</codePool>
  7.         </MagentoBoy_StarRating>
  8.     </modules>
  9. </config>
并添加配置文件:
/app/code/local/MagentoBoy/StarRating/etc/config.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MagentoBoy_StarRating>
  5.             <version>0.1.0</version>
  6.         </MagentoBoy_StarRating>
  7.     </modules>
  8. </config>
3 添加Layout文件
/app/design/frontend/default/default/starrating.xml
  1. <?xml version="1.0"?>
  2. <layout>
  3.     <review_product_list>
  4.         <reference name="head">
  5.             <action method="addItem"><type>js</type><name>star_rating/jquery.rating.js</name></action>
  6.             <action method="addItem"><type>js_css</type><name>star_rating/jquery.rating.css</name></action>
  7.         </reference>
  8.         <reference name="product.review.form">
  9.             <action method="setTemplate"><template>starrating/form.phtml</template></action>
  10.         </reference>
  11.     </review_product_list>
  12. </layout>
并在config.xml中添加layout文件
  1. <config>
  2.     <frontend>
  3.         <layout>
  4.             <updates>
  5.                 <starrating>
  6.                     <file>starrating.xml</file>
  7.                 </starrating>
  8.             </updates>
  9.         </layout>
  10.     </frontend>
  11. </config>
4 修改template文件
复制
/app/design/frontend/base/default/template/review/form.phtml
/app/design/frontend/default/default/template/starrating/form.phtml

  1. <table class="data-table" id="product-review-table">
  2.     <col />
  3.     <col width="1" />
  4.     <col width="1" />
  5.     <col width="1" />
  6.     <col width="1" />
  7.     <col width="1" />
  8.     <thead>
  9.         <tr>
  10.             <th>&nbsp;</th>
  11.             <th><span class="nobr"><?php echo $this->__('1 star') ?></span></th>
  12.             <th><span class="nobr"><?php echo $this->__('2 stars') ?></span></th>
  13.             <th><span class="nobr"><?php echo $this->__('3 stars') ?></span></th>
  14.             <th><span class="nobr"><?php echo $this->__('4 stars') ?></span></th>
  15.             <th><span class="nobr"><?php echo $this->__('5 stars') ?></span></th>
  16.         </tr>
  17.     </thead>
  18.     <tbody>
  19.     <?php foreach ($this->getRatings() as $_rating): ?>
  20.         <tr>
  21.             <th><?php echo $this->escapeHtml($_rating->getRatingCode()) ?></th>
  22.         <?php foreach ($_rating->getOptions() as $_option): ?>
  23.             <td class="value"><input type="radio" name="ratings[<?php echo $_rating->getId() ?>]" 
  24. id="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>" 
  25. value="<?php echo $_option->getId() ?>" class="radio" /></td>
  26.         <?php endforeach; ?>
  27.         </tr>
  28.     <?php endforeach; ?>
  29.     </tbody>
  30. </table>
修改为
  1. <table class="data-table" id="product-review-table">
  2.     <tbody>
  3.     <?php foreach ($this->getRatings() as $_rating): ?>
  4.         <tr>
  5.             <td><?php echo $this->escapeHtml($_rating->getRatingCode()) ?></td>
  6.             <td class="value">
  7.                 <?php foreach ($_rating->getOptions() as $_option): ?>
  8.                 <input type="radio" name="ratings[<?php echo $_rating->getId() ?>]" 
  9. id="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>"
  10.  value="<?php echo $_option->getId() ?>" class="star" />
  11.                 <?php endforeach; ?>
  12.             </td>
  13.         </tr>
  14.     <?php endforeach; ?>
  15.     </tbody>
  16. </table>
  17. <script type="text/javascript">//<![CDATA[
  18. var $j = jQuery.noConflict();
  19. $j(document).ready(function(){
  20.     $('.star').rating();
  21. });
  22. //]]></script>
清除缓存,刷新产品页面,可以看到Star-Rating插件已经加载成功了。
0 0
原创粉丝点击