Labo

[EC-CUBE 2.11.4] レビュー一覧ブロック作成

2012年02月05日 / 投稿者名:fukap


レビュー関係では以前、下のようなエントリーを書いたのですが。

 

 レビュー一覧ページ作成 https://ec-cube.nakweb.com/blog/408.html
 レビュー一覧ページ作成 その2 https://ec-cube.nakweb.com/blog/447.html

 

そういえばレビュー一覧ブロックを作ってなかったので、作ってみました。

 

 

要領はレビュー一覧ページ作成とほとんど同じなんですが、ブロックとして作成するところがちょっと違います。

 

1.html/frontparts/bloc/review.php を新規作成

<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

// {{{ requires
require_once realpath(dirname(__FILE__)) . '/../../require.php';
require_once CLASS_EX_REALDIR . 'page_extends/frontparts/bloc/LC_Page_FrontParts_Bloc_Review_Ex.php';

// }}}
// {{{ generate page

$objPage = new LC_Page_FrontParts_BLoc_Review_Ex();
$objPage->blocItems = $params['items'];
register_shutdown_function(array($objPage, "destroy"));
$objPage->init();
$objPage->process();
?>

2.data/class_extends/page_extends/frontparts/bloc/LC_Page_FrontParts_Bloc_Review_Ex.php を新規作成

<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

// {{{ requires
require_once CLASS_REALDIR . 'pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Review.php';

/**
 * Review のページクラス(拡張).
 *
 * LC_Page_FrontParts_Bloc_Review をカスタマイズする場合はこのクラスを編集する.
 *
 * @package Page
 */
class LC_Page_FrontParts_Bloc_Review_Ex extends LC_Page_FrontParts_Bloc_Review {

    // }}}
    // {{{ functions

    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {
        parent::init();
    }

    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        parent::process();
    }

    /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }
}
?>

3.data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Review.php を新規作成

<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

// {{{ requires
require_once CLASS_REALDIR . 'pages/frontparts/bloc/LC_Page_FrontParts_Bloc.php';

/**
 * Review のページクラス.
 *
 * @package Page
 */
class LC_Page_FrontParts_Bloc_Review extends LC_Page_FrontParts_Bloc {

    // }}}
    // {{{ functions

    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {
        parent::init();
        $masterData = new SC_DB_MasterData_Ex();
        $this->arrRECOMMEND = $masterData->getMasterData("mtb_recommend");
    }

    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        $this->action();
        $this->sendResponse();
    }

    /**
     * Page のアクション.
     *
     * @return void
     */
    function action() {
        //レビュー情報の取得
        $this->arrReview = $this->lfGetReviewData();
    }    
  /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }

    /**
     * レビュー検索.
     *
     * @return array $arrReview 検索結果配列
     */
    function lfGetReviewData() {
        $objQuery =& SC_Query_Ex::getSingletonInstance();
        //商品ごとのレビュー情報を取得する
        $col = "create_date, reviewer_url, reviewer_name, recommend_level, title, comment";
        $from = "dtb_review";
        $where = "del_flg = 0 AND status = 1 ORDER BY create_date DESC";
        $arrReview = $objQuery->select($col, $from, $where);
        return $arrReview;
    }
}
?>

4.data/Smarty/templates/default/frontparts/bloc/review.tpl を新規作成

<div id="customervoice_area">
<h2><img src="<!--{$TPL_URLPATH}-->img/title/tit_product_voice.jpg" alt="この商品に対するお客様の声" /></h2>
    <!--{if count($arrReview) > 0}-->
        <ul>
            <!--{section name=cnt loop=$arrReview}-->
                <li>
                    <p class="voicetitle"><!--{$arrReview[cnt].title|h}--></p>
                    <p class="voicedate"><!--{$arrReview[cnt].create_date|sfDispDBDate:false}--> 投稿者:<!--{if $arrReview[cnt].reviewer_url}--><a href="<!--{$arrRe
view[cnt].reviewer_url}-->" target="_blank"><!--{$arrReview[cnt].reviewer_name|h}--></a><!--{else}--><!--{$arrReview[cnt].reviewer_name|h}--><!--{/if}--> おすすめレ>
ベル:<span class="recommend_level"><!--{assign var=level value=$arrReview[cnt].recommend_level}--><!--{$arrRECOMMEND[$level]|h}--></span></p>
                    <p class="voicecomment"><!--{$arrReview[cnt].comment|h|nl2br}--></p>
                </li>
            <!--{/section}-->
        </ul>
    <!--{/if}-->
</div>

5.データベースに新規ブロックを登録します。
  phpMyAdmin や phpPgAdmin などデータベース編集ツールを使用して、 dtb_bloc テーブル内のデータを確認します。
  device_type_id が 10 のレコードで、一番数字の大きい bloc_id に +1 した数字を、今回の新規ブロックの bloc_id にします。
  bloc_id が決まったら、以下のような感じで新しいレコードを挿入します。

 

  device_type_id 10
  bloc_id 10
  bloc_name レビュー
  tpl_path review.tpl
  filename review
  create_date now()
  update_date now()
  php_path frontparts/bloc/review.php
  deletable_flg 0

 

  SQLを直接流す場合はこんな感じですかね。
  insert into dtb_bloc values(10,10,’レビュー’,’review.tpl’,’review’,’now()’,’now()’,’frontparts/bloc/review.php’,0);

 

6.ここまでできたら、管理画面にログインして、デザイン管理>PC>レイアウト設定 で、
  未使用ブロックに「レビュー」が追加されているのを確認します。
  トップページの真ん中列、新着情報ブロックの下などにドラッグ&ドロップで配置して、ブラウザでトップページを確認してください。
  なお、タイトル画像は「この商品に対するお客様の声」になっていると思うので、適当に代わりの画像を用意してください。

 

 

(おまけ)
 レビュー一覧から、あまりひどいレビューを除きたいという場合もあるかもしれません。
 その場合は、以下のように recommend_level を抽出条件に設定すると実現できます。
 recommend_level >= 3 なので、星3つ以上ということです。

 

data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Review.php の lfGetReviewData() にて。

    function lfGetReviewData() {
        $objQuery =& SC_Query_Ex::getSingletonInstance();
        //商品ごとのレビュー情報を取得する
        $col = "create_date, reviewer_url, reviewer_name, recommend_level, title, comment";
        $from = "dtb_review";
        $where = "del_flg = 0 AND status = 1 AND recommend_level >= 3 ORDER BY create_date DESC";
        $arrReview = $objQuery->select($col, $from, $where);
        return $arrReview;
    }

[EC-CUBE 2.11.4] レビュー一覧ブロック作成 への7件のコメント

  1. 岸本 より:

    はじめまして、独学でEC-CUBEをいじっております。

    レビューに関して「一覧ページ作成 その2」まではうまくいったのですが、こちらの「レビュー一覧ブロック作成」がうまくいかなかったので質問させてください。

    6.までページに書いてある通りにやったつもりなのですが、管理画面でレビューブロックを配置して、ブラウザでトップページを確認したところ、ブロックを配置した位置に下記のエラーが出てしまいました。

    Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in ●●●●●●●/●●●●/data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Review.php on line 67

    エラーが出ないようにして、レビューを表示するにはどのようにしたら良いのかアドバイスをいただけたらありがたいです。よろしくお願い致します。

  2. fukap より:

    すいません、確認遅くなりました^^;
    まだエラーは続いていますか?

    LC_Page_FrontParts_Bloc_Review.php の 67行目でエラーが出ているようです。
    67行目だけでなく、その前後を細かくチェックしてみてください。
    全角スペースが入っていても、確かこのエラーが出たように思います。

  3. 岸本 より:

    ご返信ありがとうございました。

    ご指摘の通り、LC_Page_FrontParts_Bloc_Review.php の 67行目付近に全角スペースが入っておりました。

    全角スペースを削除したところ、きちんと表示されました。

    ありがとうございました!

  4. より:

    いつも楽しく拝見させていただいております。
    本エントリーを参考にブロックを作成させて頂きたい変助かりました。

    ご質問なのですがこのブロックにその商品の画像やリンクを表示させるにはどうしたら良いかご教授頂けないでしょうか。

    宜しくお願いします。

  5. fukap より:

    function lfGetReviewData() で dtb_review から値を取得していますが、
    dtb_review には商品画像に関する情報がありません。
    ですので、dtb_products から情報を取得する必要がありますね。
    以下のような感じで書き換えてみるとどうでしょうか。

    $col = "r.create_date, r.reviewer_url, r.reviewer_name, r.recommend_level, r.title, r.comment, p.main_image";
    $from = "dtb_review as r left join dtb_products as p on r.product_id = p.product_id";

    あとは review.tpl をカスタマイズしてみてください!

  6. より:

    早速のご返信ありがとうございます。本日data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Review.phpを書き換えて試させていただきましたが「システムエラーが発生しました。大変お手数ですが、サイト管理者までご連絡ください。」が表示されてしまいました。自分がphpに対する知識が不足しているため原因がわかりませんでした。。もしお時間がある時で構いませんので考えられる原因についてご連絡頂けますと幸いです。宜しくお願い致します。

  7. fukap より:

    where句も書き換えないといけなかったですね。
    こんな感じかな。

    $where = "r.del_flg = 0 AND r.status = 1 AND r.recommend_level >= 3 ORDER BY r.create_date DESC";

へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

* Copy This Password *

* Type Or Paste Password Here *

*

コメント欄にコードを挿入したい場合は、[php][/php] を使ってください。