[EC-CUBE]人気ランキングを表示する

売れ筋商品をランキング形式で表示させる。

売上の1~5位をランキング形式で表示。
こんな感じで。

では、さっそくカスタマイズ。
/html/frontparts/bloc/rankig.php
インクルードしているファイルの名前を変更する
  1. <?php
  2. require_once(CLASS_EX_PATH . "page_extends/frontparts/bloc/LC_Page_FrontParts_Bloc_Ranking_Ex.php");
  3.  
  4. $objPage = new LC_Page_FrontParts_Bloc_Ranking_Ex();
  5. register_shutdown_function(array($objPage, "destroy"));
  6. $objPage->init();
  7. $objPage->process();
  8. ?>
  9.  

/data/class_extends/page_extends/frontparts/bloc/LC_Page_FrontParts_Bloc_Ranking_Ex.php
これまたインクルード先を変える。
  1. <?php
  2. require_once(CLASS_PATH . "pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Ranking_Ex.php");
  3.  
  4. /**
  5.  * ランキング のページクラス(拡張).
  6.  *
  7.  * LC_Page_FrontParts_Bloc_Ranking をカスタマイズする場合はこのクラスを編集する.
  8.  */
  9. class LC_Page_FrontParts_Bloc_Ranking_Ex extends LC_Page_FrontParts_Bloc_Ranking {
  10. /**
  11.   * Page を初期化する.
  12.   *
  13.   * @return void
  14.   */
  15. function init() {
  16. parent::init();
  17. }
  18.  
  19. /**
  20.   * Page のプロセス.
  21.   *
  22.   * @return void
  23.   */
  24. function process() {
  25. parent::process();
  26. }
  27.  
  28. /**
  29.   * デストラクタ.
  30.   *
  31.   * @return void
  32.   */
  33. function destroy() {
  34. parent::destroy();
  35. }
  36. }
  37. ?>
  38.  

/data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Ranking.php
これはBest5をコピーしてロジックを編集する。
  1. <?php
  2. require_once(CLASS_PATH . "pages/frontparts/bloc/LC_Page_FrontParts_Bloc.php");
  3.  
  4. /**
  5.  * ランキング のページクラス.
  6.  */
  7. class LC_Page_FrontParts_Bloc_Ranking extends LC_Page_FrontParts_Bloc {
  8.  
  9. /**
  10.   * Page を初期化する.
  11.   *
  12.   * @return void
  13.   */
  14. function init() {
  15. parent::init();
  16. $bloc_file = 'ranking.tpl';
  17. $this->setTplMainpage($bloc_file);
  18. }
  19.  
  20. /**
  21.   * Page のプロセス.
  22.   *
  23.   * @return void
  24.   */
  25. function process() {
  26. if (defined("MOBILE_SITE") && MOBILE_SITE) {
  27. $objView = new SC_MobileView();
  28. } else {
  29. $objView = new SC_SiteView();
  30. }
  31. $objSiteInfo = $objView->objSiteInfo;
  32.  
  33. // 基本情報を渡す
  34. $objSiteInfo = new SC_SiteInfo();
  35. $this->arrInfo = $objSiteInfo->data;
  36.  
  37. // ランキング表示 2009.05.08追加
  38. $this->arrRanking = $this->lfGetRanking();
  39.  
  40. $objView->assignobj($this);
  41. objView->display($this->tpl_mainpage);
  42. //おすすめ商品表示
  43. //$this->arrBestProducts = $this->lfGetRanking();
  44.  
  45. //$objView->assignobj($this);
  46. //objView->display($this->tpl_mainpage);
  47. }
  48.  
  49. /**
  50.   * モバイルページを初期化する.
  51.   *
  52.   * @return void
  53.   */
  54. function mobileInit() {
  55. $this->tpl_mainpage = MOBILE_TEMPLATE_DIR . "frontparts/"
  56. . BLOC_DIR . 'ranking.tpl';
  57. }
  58.  
  59. /**
  60.   * Page のプロセス(モバイル).
  61.   *
  62.   * @return void
  63.   */
  64. function mobileProcess() {
  65. $this->process();
  66. }
  67.  
  68. /**
  69.   * デストラクタ.
  70.   *
  71.   * @return void
  72.   */
  73. function destroy() {
  74. parent::destroy();
  75. }
  76.  
  77. // ランキング検索
  78. function lfGetRanking(){
  79. $objQuery = new SC_Query();
  80.  
  81. $col = "T1.product_id, T1.product_name as name, T3.main_list_image, COUNT(*) as order_count";
  82. $from = "dtb_order_detail AS T1
  83. INNER JOIN dtb_order AS T2 ON T1.order_id = T2.order_id
  84. INNER JOIN dtb_products AS T3 ON T1.product_id = T3.product_id";
  85. $objQuery->setgroupby("T1.product_id, T1.product_name, T3.main_list_image");
  86. $objQuery->setorder("order_count DESC");
  87. $objQuery->setlimit(5);
  88.  
  89. return $objQuery->select($col, $from);
  90. }
  91. }
  92. ?>
  93.  

ブロックを追加する。
管理画面>デザイン管理>ブロック編集
ブロック名 ランキング
ファイル名 ranking
※ランキング用の画像は別途用意する必要あり。
  1. <!--{if count($arrRanking) > 0}-->
  2. <h2>
  3. <img src="<!--{$TPL_DIR}-->img/side/title_ranking.jpg" width="166" height="35" alt="ランキング" />
  4. </h2>
  5. <div id="rankingarea">
  6. <ul>
  7. <!--{foreach from=$arrRanking key=myId item=i}-->
  8. <li>
  9. <a href="<!--{$smarty.const.DETAIL_P_HTML}--><!--{$i.product_id}-->">
  10. <img src="<!--{$smarty.const.IMAGE_SAVE_URL|sfTrimURL}-->/<!--{$i.main_list_image}-->" width="130px" alt="<!--{$i.name}-->">
  11. <!--{assign var=rank value=$myId+1}-->
  12. <span><!--{$rank}-->位:<!--{$i.name}--></span>
  13. </a>
  14. </li>
  15. <!--{/foreach}-->
  16. </ul>
  17. </div>
  18. <!--{/if}-->
  19.  

CSS編集
ランキング用のCSSを追加
/html/user_data/packages/default/css/main.css

  1. /* ランキング @2009.05.08追加
  2. ----------------------------------------------- */
  3. div#rankingarea {
  4. width: 144px;
  5. padding: 0 10px 10px 10px;
  6. border: solid 1px #ccc;
  7. }
  8.  
  9. div#rankingarea img {
  10. padding: 0px;
  11. margin: 2px auto;
  12. vertical-align: bottom;
  13. display: block;
  14. }
  15.  
  16. div#rankingarea ul li {
  17. margin-top: 6px;
  18. }
  19.  
  20. div#rankingarea ul li span {
  21. clear: both;
  22. }
  23.  
  24. div#rankingarea ul li a {
  25. font-weight:bold;
  26. color: red;
  27. text-decoration: none;
  28. display: block;
  29. }
  30.  
  31. div#rankingarea ul li a:hover {
  32. color: red;
  33. background-color: #DDDDDD;
  34. }
  35.  

DBに値を追加する。
dtb_blocテーブルに先ほど追加した「ランキング」ブロックのデータが反映されているので、
そこのphp_pathを以下の値で更新する。

frontparts/bloc/ranking.php
最終更新:2009年05月11日 10:30
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。
添付ファイル