Labo

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

2012年03月25日 / 投稿者名:fukap


ようやく EC-CUBE2.11.5 をテスト環境に入れてみました。
そこで時々質問が来る「新着商品を自動表示」が、2.11.5 でできるかなと試してみました。
結論から言うと、特に問題なくできたのですが、
当時のコードを見直すと、なんだか訳わからんことになっているので、
もう少しわかりやすく書き直してみました。

 

仕様は前回と同じです。
商品ステータスで「NEW」を設定した商品の中から、一定数の商品を表示させるブロックです。

 

1.html/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 realpath(dirname(__FILE__)) . '/../../require.php';
require_once(CLASS_EX_REALDIR . "page_extends/frontparts/bloc/LC_Page_FrontParts_Bloc_New_Products_Ex.php");

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

$objPage = new LC_Page_FrontParts_Bloc_New_Products_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_New_Products_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_New_Products.php");

/**
 * New_Products のページクラス(拡張).
 *
 * LC_Page_FrontParts_Bloc_New_Products をカスタマイズする場合はこのクラスを編集する.
 *
 */
class LC_Page_FrontParts_Bloc_New_Products_Ex extends LC_Page_FrontParts_Bloc_New_Products {

    // }}}
    // {{{ 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_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;

        // 新着商品のステータスIDを設定(デフォルトでは NEW=1)
        $new_product_id = 1;

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

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

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

    /**
     * 新着商品取得.
     *
     * @param int 新着商品のステータスID
     * @return array 新着商品配列
     */
    function getNewProducts($new_product_id, $limit){
        $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 ps.product_status_id = ?";
        $groupby = "p.product_id, p.name, p.main_list_image, p.main_list_comment, ps.product_id, p.update_date";
        $objQuery->setGroupBy($groupby);
        $objQuery->setOrder('p.update_date DESC');
        $objQuery->setLimit($limit);

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

?>

4.data/Smarty/templates/default/frontparts/bloc/new_products.tpl を作成(CSSは適宜修正してください。)

<!--{if count($arrNewProducts) > 0}-->
<div class="bloc_outer clearfix">
    <div id="recommend_area">
        <h2>新着商品</h2>
        <!--{section name=cnt loop=$arrNewProducts step=2}-->
        <div class="bloc_body clearfix">
            <div class="product_item clearfix">
                <div class="productImage">
                    <a href="<!--{$smarty.const.P_DETAIL_URLPATH}--><!--{$arrNewProducts[cnt].product_id|u}-->">
                        <img src="<!--{$smarty.const.ROOT_URLPATH}-->resize_image.php?image=<!--{$arrNewProducts[cnt].main_list_image|sfNoImageMainList|h}-->&amp;width=80&amp;height=80" alt="<!--{$arrNewProducts[cnt].name|h}-->" /></a>
                </div>
                <div class="productContents">
                    <h3>
                        <a href="<!--{$smarty.const.P_DETAIL_URLPATH}--><!--{$arrNewProducts[cnt].product_id|u}-->"><!--{$arrNewProducts[cnt].name|h}--></a>
                    </h3>
                    <!--{assign var=price01 value=`$arrNewProducts[cnt].price01_min`}-->
                    <!--{assign var=price02 value=`$arrNewProducts[cnt].price02_min`}-->
                    <p class="sale_price"><!--{$smarty.const.SALE_PRICE_TITLE}-->(税込):
                        <span class="price"><!--{$price02|sfCalcIncTax:$arrInfo.tax:$arrInfo.tax_rule|number_format}--> 円</span>
                    </p>
                    <p class="mini comment"><!--{$arrNewProducts[cnt].comment|h|nl2br}--></p>
                </div>
            </div>

            <div class="product_item clearfix">
                <div class="productImage">
                    <!--{assign var=cnt2 value=`$smarty.section.cnt.iteration*$smarty.section.cnt.step-1`}-->
                    <!--{if $arrNewProducts[$cnt2]|count > 0}-->

                    <a href="<!--{$smarty.const.P_DETAIL_URLPATH}--><!--{$arrNewProducts[$cnt2].product_id|u}-->">
                    <img src="<!--{$smarty.const.ROOT_URLPATH}-->resize_image.php?image=<!--{$arrNewProducts[$cnt2].main_list_image|sfNoImageMainList|h}-->&amp;width=80&amp;height=80" alt="<!--{$arrNewProducts[$cnt2].name|h}-->" /></a>
                </div>
                <div class="productContents">
                    <h3>
                        <a href="<!--{$smarty.const.P_DETAIL_URLPATH}--><!--{$arrNewProducts[$cnt2].product_id|u}-->"><!--{$arrNewProducts[$cnt2].name|h}--></a>
                    </h3>

                    <!--{assign var=price01 value=`$arrNewProducts[$cnt2].price01_min`}-->
                    <!--{assign var=price02 value=`$arrNewProducts[$cnt2].price02_min`}-->

                    <p class="sale_price"><!--{$smarty.const.SALE_PRICE_TITLE}-->(税込):
                        <span class="price"><!--{$price02|sfCalcIncTax:$arrInfo.tax:$arrInfo.tax_rule|number_format}--> 円</span>
                    </p>
                    <p class="mini comment"><!--{$arrNewProducts[$cnt2].comment|h|nl2br}--></p>
                    <!--{/if}-->
                </div>
            </div>

        </div>
        <!--{/section}-->
    </div>
</div>
<!--{/if}-->

5.データベース(dtb_bloc)に新規ブロック用のレコードを登録します。phpMyAdminなどを使って行ってください。
  bloc_id は、他のブロックと重複しないIDにしてください。

 

カラム名
device_type_id 10
bloc_id 10
bloc_name new_products
tpl_path new_products.tpl
filename new_products
create_date now()
update_date now()
php_path frontparts/bloc/new_products.php
deletable_flg 0

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

  1. ピンバック: [EC-CUBE 2.11.1] 新着商品を自動表示 | NAKWEB × EC-CUBE

  2. ピンバック: EC-CUBE NEWS | EC-CUBEに関するニュースやブログの最新情報をお届けします

  3. ピンバック: [EC-CUBE 2.11.5] 新着商品を表示する その2 | NAKWEB × EC-CUBE

  4. ピンバック: [EC-CUBE 2.11.5] 新着商品を表示する その3 | NAKWEB × EC-CUBE

  5. chocomame73 より:

    できました!
    今独学でEC-CUBEを試している所です。PHPやデータベースが初心者で湧かないのでこのように丁寧にわかりやすく書かれているととても助かります。

    2つ質問があります。
    新商品を登録した後、NEWのチェックを外しても表示されてしまいます。NEWのチェックを外したら新着商品から外れるようにしたいのですがどのようにしたらできるのでしょうか。
    私の設定のミスなのでしょうか。

    もう1点。
    掲載数を8点に設定しました。
    全商品8点に対して新商品が3点という状態で試しました。
    7点表示されました。3点以外はNEWのチェックが入っていません。
    新商品が8点に達していない場合は、ランダムに表示されるのでしょうか?

    お手数をおかけいたしますが
    教えていただけないでしょうか。よろしくお願いいたします。

  6. 4pain8pain より:

    お世話になります。

    当サイトの情報により、PCサイトにて新着商品ブロックを表示させることができました。ありがとうございます。

    この新着商品を横に4列に表示させたくてECCubeの開発コミュニティにある以下を参考に致しました。結果はIE6,7では期待通り横4列に表示されるのですが、Firefox,Safari等では横2列に表示されます。

    http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=10468&forum=10

    どこでどう調整すれば宜しいかお分かりになりますでしょうか。

    一応、block.cssファイルで、firefox、Safari様のハックなどを施し試しましたが、機能しない模様でございます。

  7. fukap より:

    chocomame73 様

    すいません、ご返事できずだいぶ時間が経過してしまいました。
    NEWのチェックを外すと、新着商品からはずれるとは思うのですが、まだ問題は継続していますか?

  8. fukap より:

    4pain8pain 様

    キャッシュが影響しているということはありませんか?
    他PCでも2列表示になりますか?

  9. 4pains8pains より:

    fukap様

    返信感謝致します。

    他の3台のPCで試しましたが、結果は同じで、IE系は4列表示されますが、Firefox、Safariは2列表示で残りが次の行に表示されます。

  10. fukap より:

    4pain8pain 様

    こういうケースでは、Forefoxプラグインの Fireebugなどを使って、
    地道にHTMLやCSSを見直していくしかないと思います。

  11. sakura より:

    はじめまして、sakuraと申します。

    いつも参考にさせていただいております。

    以前にお作りになった「[EC-CUBE 2.11.1] 新着商品を表示」では、新着商品のランダム表示があったと思うのですが、「[EC-CUBE 2.11.5] 新着商品を表示する」で、ランダム表示するにはどのようにすればいいでしょうか。

    ご回答いただけると、非常にありがたいです。

  12. fukap より:

    そういえばランダム表示がありましたね。
    自分としては意義を感じていなかったので、はずしてしまいましたが。

    以前のコードを見ればわかりますが、ランダム表示は以下の部分で実現されていたと思います。

            //商品の表示をランダムに抽出する場合は以下のコメントを外してください。
            //srand((double)microtime()*1000000);  //乱数生成器を初期化
            //shuffle($arrFlagList);

    なのでこのコードを付けてあげれば良いと思います。
    ただし、$arrFlagList の代わりに、$arrNewProducts とかを使うといいですかね。
    たぶん、以下のような感じで動くんじゃないかな。

        function action() {
    
            // 基本情報を渡す
            $objSiteInfo = SC_Helper_DB_Ex::sfGetBasisData();
            $this->arrInfo = $objSiteInfo->data;
    
            // 新着商品のステータスIDを設定(デフォルトでは NEW=1)
            $new_product_id = 1;
    
            //表示する商品の件数
            $limit = 4;
    
            // 新着商品取得
            $arrNewProducts = $this->getNewProducts($new_product_id, $limit);
    
            //商品の表示をランダムに抽出する場合は以下のコメントを外してください。
            //srand((double)microtime()*1000000);  //乱数生成器を初期化
            //shuffle($arrNewProducts);
    
            $this->arrNewProducts = $arrNewProducts;
        }
  13. sakura より:

    fukap 様

    ありがとうございます。

    ランダム表示成功しました。

    これからも、こちらのサイトを参考にさせていたきますので、頑張ってください。

  14. tuya より:

    はじめまして、 tuyaと申します。
    Verは[2.12.2]を使用しております。
    以前から参考にさせていただいております。
    new_products.tpl の記載で

    <!--{assign var=price02 value=`$arrNewProducts[cnt].price02_min`}-->

    と販売価格を表示していますが
    この箇所を条件分岐にしたいのですが
    可能でしょうか。

    例えば1とか2とかの値を判定して
    “ワケあり”とか”交渉可”など
    text表示させたいと思っています。

    ご回答いただければ、非常にありがたいです。
    よろしくお願いいたします。

  15. fukap より:

    2.12.2 とかになると、良いプラグインとかないですか?
    探せばありそうな気がしますが。

    ちなみにご質問の条件分岐ですが、難しくはないと思いますよ。
    販売価格の値によって、条件分岐したいということでしょうか。
    もしそうであれば、

    <!--{if $price02 == 1}-->
     1の場合の表示
    <!--{elseif $price02 == 2}-->
     2の場合の表示
    <!--{else}-->
     それ以外の表示
    <!--{/if}-->
    

    って感じじゃないですかね、たぶん。

  16. tuya より:

    fukap 様
    ありがとうございます。

    教えていただいたものを
    税込価格部分

    に記載する事で無事表示させる事ができました。

    考えているものに近づけそうです。

    一部デフォルトの表示(税込価格表示)が
    でているのですが
    これはキャッシュによるものでしょうか。

    これは自分でもうちょっと調べてみます。

    これからも参考にさせていただきたいと思います。ありがとうございました。

  17. tuya より:

    fukap 様 お世話になっております。

    以前の記載
    https://ec-cube.nakweb.com/blog/318.html

    でのご質問のように
    category_nameを取得表示したいのですが
    同じ様に$col、$fromへの記載が
    必要になりますでしょうか。

    記載方法がわからず質問させていただきました。

    お手数をおかけしますが
    教えていただけませんでしょうか。

    よろしくお願いいたします。

  18. fukap より:

    $col, $from への記載は必要だと思います。
    以下のような感じでしょうか。希望のものと異なるかもしれませんが。
    これ以上はご勘弁を。

        /**
         * 新着商品取得.
         *
         * @param int 新着商品のステータスID
         * @return array 新着商品配列
         */
        function getNewProducts($new_product_id, $limit){
            $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,
    		MIN(c.category_name) as category_name
    __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
    	   LEFT JOIN dtb_product_categories as pc2
    	     ON p.product_id = pc2.product_id
    	   LEFT JOIN dtb_category as c
    	     ON pc2.category_id = c.category_id
    __EOS__;
            $where = "p.del_flg = 0 AND p.status = 1 AND ps.product_status_id = ?";
            $groupby = "p.product_id, p.name, p.main_list_image, p.main_list_comment, ps.product_id, p.update_date";
            $objQuery->setGroupBy($groupby);
            $objQuery->setOrder('p.update_date DESC');
            $objQuery->setLimit($limit);
    
            return $objQuery->select($col, $from, $where, array($new_product_id));
        }
  19. tuya より:

    fukap 様

    お忙しいところ
    回答いただきありがとうございました。

    教えていただいた表記で
    取得表示させる事ができました。

    丁寧に教えていただいて
    本当にありがとうございました。

  20. maru より:

    fukap様

    価格の箇所ですが、規格を2種類選択させた場合は
    上手く表示されず0円になってしまうのですが
    どのように修正したらよいのでしょうか。
    何卒よろしくお願致します。

  21. fukap より:

    maru様

    試していないので、間違っているかもしれませんが。

    data/Smarty/templates/default/frontparts/bloc/new_products.tpl の17行目あたりを
    以下のような感じに書き換えると、うまくいきませんかね?

    <!--{assign var=price01 value=`$arrNewProducts[cnt].price01_min`}-->
    <!--{assign var=price02 value=`$arrNewProducts[cnt].price02_min`}-->
    <!--{assign var=price02_max value=`$arrNewProducts[cnt].price02_max`}-->
    <p class="sale_price"><!--{$smarty.const.SALE_PRICE_TITLE}-->(税込):
    	<span class="price">
    		<!--{if price02 != price02_max}-->
    			<!--{$price02_max|sfCalcIncTax:$arrInfo.tax:$arrInfo.tax_rule|number_format}-->~
    		<!--{/if}-->
    			<!--{$price02|sfCalcIncTax:$arrInfo.tax:$arrInfo.tax_rule|number_format}--> 円
        	</span>
    </p>
  22. matsu より:

    お世話になります。
    ecキューブ初心者になります。
    こちらのコードをコピペし、動かしたところ下記エラーがでて表示ができません。
    —————

    ▼▼▼ デバッグ情報ここから ▼▼▼
    
    http://127.0.0.1/eccube/html/preview/?filename=index&transactionid=1d262e887504ef6dcdf0948c8b582de6e2b3fcb2?filename=index&transactionid=1d262e887504ef6dcdf0948c8b582de6e2b3fcb2
    
    SERVER_ADDR: 127.0.0.1
    REMOTE_ADDR: 127.0.0.1
    USER_AGENT: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
    
    Fatal error(E_COMPILE_ERROR): require_once(): Failed opening required 'C:/xampp/htdocs/eccube/html/../data/class_extends/page_extends/frontparts/bloc/LC_Page_FrontParts_Bloc_New_Products_Ex.php' (include_path='C:\xampp\htdocs\eccube\data\module;.;C:\xampp\php\PEAR') on [C:\xampp\htdocs\eccube\html\frontparts\bloc\new_products.php(24)]
    
    ▲▲▲ デバッグ情報ここまで ▲▲▲
    

    —————
    ecキューブのバージョンは 2.13.2になります。
    ご教示頂ければ幸いです。

  23. fukap より:

    matsuさん、こんにちは。
    お使いのバージョンは2.13.2とのことですので、
    コードの読み替えが必要になってくると思います。

  24. matsu より:

    fukap様

    ご回答ありがとうございます。
    コードの読み替えが必要とのことですが、具体的にどのようにすればよろしいのでしょうか?
    無知ですみません。。

  25. fukap より:

    考え方はそのまま利用できると思いますが、
    お使いの2.13.2はコード内容が2.11.5とは異なっていますので、
    コードを書き換える必要があります。

    2.13.2用にコードを書き直す予定は今のところございませんので、
    どうしてもご入用でしたら有償でご依頼いただきますようお願いいたします。

  26. matsu より:

    fukap様

    ご回答ありがとうございます。
    一度、自分でやってみて、難しそうな場合はご依頼するかとおもいます。

    その際は、よろしくお願いいたいます。

  27. Ken より:

    fukap様

    新着商品ブロックの商品削除に関するご質問でございます。

    管理者画面から登録した商品を削除いたしますと、サイト上ではデフォルトで最初からある商品一覧、あるいはおすすめ商品ブロックの商品を削除いたしますが、新たに付け加えた本件のような新着商品ブロックの商品を削除するのでしょうか。

    管理者画面の商品削除によって、新着商品ブロックの商品も削除させるにはどうしたら宜しいのでしょうか。

  28. Ken より:

    fukap様

    大変失礼いたしました。
    自己解決いたしました。

  29. より:

    税込価格となっておりますが、表示は税別価格だと思うのですが?

  30. yuzu より:

    ご指摘ありがとうございます。

    確認させていただきます。

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

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

* Copy This Password *

* Type Or Paste Password Here *

*

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