Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273573
  • 博文数量: 59
  • 博客积分: 1368
  • 博客等级: 中尉
  • 技术积分: 1071
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-02 06:06
文章分类

全部博文(59)

文章存档

2012年(59)

我的朋友

分类: 系统运维

2012-03-04 02:19:02

在Magento产品页,用户可以对产品进行评级,Magento默认使用Radio让用户选择评分等级,用户体验不是很好,基于jQuery的星级评分插件有很多,这里我们使用这款插件。

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[getId() ?>]" id="escapeHtml($_rating->getRatingCode()) ?>_getValue() ?>" value="getId() ?>" class="radio" /></td>
  24.         <?php endforeach; ?>
  25.         </tr>
  26.     <?php endforeach; ?>
  27.     </tbody>
  28. </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[getId() ?>]" id="escapeHtml($_rating->getRatingCode()) ?>_getValue() ?>" value="getId() ?>" class="star" />
  9.                 <?php endforeach; ?>
  10.             </td>
  11.         </tr>
  12.     <?php endforeach; ?>
  13.     </tbody>
  14. </table>
  15. <script type="text/javascript">//<![CDATA[
  16. var $j = jQuery.noConflict();
  17. $j(document).ready(function(){
  18.     $('.star').rating();
  19. });
  20. //]]></script>
清除缓存,刷新产品页面,可以看到Star-Rating插件已经加载成功了。

阅读(3266) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~