Labo

[EC-CUBE 2.11.5] 新着商品を表示する その3

2012年04月15日 / 投稿者名:fukap


前回、商品登録日を基準に最新4件を表示する新着商品ブロックを紹介しましたが、その続き。
さらに、登録してから一週間以内の商品のみ表示するという条件を加えてみたいと思います。

 

まずは、[EC-CUBE 2.11.5] 新着商品を表示する に従って、各ファイルを作成します。
3番で作成する data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_New_Products.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';

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

    // }}}
    // {{{ functions

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

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

    /**
     * Page のアクション.
     *
     * @return void
     */
    function action() {
        // 基本情報を渡す
        $objSiteInfo = SC_Helper_DB_Ex::sfGetBasisData();
        $this->arrInfo = $objSiteInfo->data;

        //表示する商品の件数
        $limit = 4;

        // 集計期間(日数)
        $period = 7;

        // 新着商品取得
        $this->arrNewProducts = $this->getNewProducts($limit, $period);
    }

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

    /**
     * 新着商品取得.
     *
     * @param int 新着商品のステータスID
     * @return array 新着商品配列
     */
    function getNewProducts($limit, $period){
        $objQuery   =& SC_Query_Ex::getSingletonInstance();
        $col = <<< __EOS__
                p.product_id,
                p.name,
                p.main_list_image,
                p.main_list_comment AS comment,
                MIN(pc.price02) AS price02_min,
                MAX(pc.price02) AS price02_max
__EOS__;
        $from = <<< __EOS__
                dtb_products as p
           LEFT JOIN dtb_products_class as pc
             ON p.product_id = pc.product_id
           LEFT JOIN dtb_product_status as ps
             ON p.product_id = ps.product_id
__EOS__;
        $where = "p.del_flg = 0 AND p.status = 1 AND p.create_date >= ?";
        $arrval[] = date("Y-m-d 00:00:00", time() - 60 * 60 * 24 * $period);

        $groupby = "p.product_id, p.name, p.main_list_image, p.main_list_comment, ps.product_id, p.create_date";
        $objQuery->setGroupBy($groupby);
        $objQuery->setOrder('p.create_date DESC');
        $objQuery->setLimit($limit);

        return $objQuery->select($col, $from, $where, $arrval);
    }
}

?>

たぶんこれで登録してから一週間以上経過した商品については、勝手に消えてくれるんじゃないかなーと。

[EC-CUBE 2.11.5] 新着商品を表示する その3 への6件のコメント

  1. neko より:

    お世話になります
    こちらのコードを使用させていただいたところ
    このようなエラーがでてしまうのですが、

    SQL: SELECT p.product_id,

    p.name,
    p.main_list_image,
    p.main_list_comment AS comment,
    MIN(pc.price02) AS price02_min,
    MAX(pc.price02) AS price02_max
    FROM dtb_products as p
    LEFT JOIN dtb_products_class as pc
    ON p.product_id = pc.product_id
    LEFT JOIN dtb_product_status as ps
    ON p.product_id = ps.product_id WHERE p.del_flg = 0 AND p.status = 1 AND p.create_date >= $1 GROUP BY p.product_id, p.name, p.main_list_image, p.main_list_comment, ps.product_id, p.create_date ORDER BY p.create_date DESC LIMIT 4

    PlaceHolder: array (
    )

  2. fukap より:

    ご報告ありがとうございます。
    執筆直前に直したコードが記事に反映されていませんでした。
    4月26日10:30に以下の修正を行いました。

     

    x まずは、[EC-CUBE 2.11.1] 新着商品を表示 に従って、各ファイルを作成します。
    o まずは、[EC-CUBE 2.11.5] 新着商品を表示する に従って、各ファイルを作成します。

     

    data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_New_Products.php
    116L

     

    x return $objQuery->select($col, $from, $where);
    o return $objQuery->select($col, $from, $where, $arrval);

  3. bluesky5 より:

    初めまして。そしてお世話になります。

    解説通りに設定(頂きました構文をそのままコピー貼り付け、左端の数字は除去)致しましたが、サイト上で以下のエラーが発生致します。何かお分かりになりますでしょうか。ご多忙の中恐縮でございます。

    “Parse error: syntax error, unexpected $end in /var/www/vhosts/aaaaaa.com/httpdocs/c/data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_New_Products.php on line 120”

  4. fukap より:

    こんにちは。
    左端の数字は除去、というあたりが怪しいですね。
    コードが表示されているエリアの上にマウスポインタを置くと、
    エリアの右上に4つのアイコンが出ます。
    そのうちの一番左の「ソースを表示」ボタンを押すと、
    ソースだけが表示されますので、
    それをもう一度使ってみてください。

    文法エラーが出ていますので、変な全角スペースが入っているとか、
    そういう理由が考えられそうです。

  5. bluesky5 より:

    fukap様

    早速のご連絡、そしてご教授感謝致します。ご指摘の通り実践したところ、見事に表示されました。時間をかけだだけにその瞬間の感動たるや・・・

    現在、携帯とスマートフォンでも同じものを表示させたく、同じファイルを使用し試しているのですが、なかなか表示されません。これは無理なのでしょうか。

  6. fukap より:

    無理ってことはないと思います。
    ただテンプレートは多少読み替えが必要になるかなと思います。

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

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

* Copy This Password *

* Type Or Paste Password Here *

*

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