Labo

[EC-CUBE 2.12.2] おすすめ商品ブロックに通常価格と割引率を表示

2012年09月16日 / 投稿者名:fukap


先週、おすすめ商品ブロックに商品ステータスアイコンを表示するカスタマイズをやってみたわけですが、
今回は第二弾で、通常価格と割引率を表示してみようかなと思いました。

 

data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Recommend.php のコードを以下のように修正(全文掲載)

<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2012 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_EX_REALDIR . 'page_extends/frontparts/bloc/LC_Page_FrontParts_Bloc_Ex.php';

/**
 * Recommend のページクラス.
 *
 * @package Page
 * @author LOCKON CO.,LTD.
 * @version $Id: LC_Page_FrontParts_Bloc_Best5 - Copy.php -1   $
 */
class LC_Page_FrontParts_Bloc_Recommend extends LC_Page_FrontParts_Bloc_Ex {

    // }}}
    // {{{ functions

    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {
        parent::init();
        $masterData                 = new SC_DB_MasterData_Ex();
        $this->arrSTATUS            = $masterData->getMasterData('mtb_status');
        $this->arrSTATUS_IMAGE      = $masterData->getMasterData('mtb_status_image');
    }
    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        $this->action();
        $this->sendResponse();
    }

    /**
     * Page のアクション.
     *
     * @return void
     */
    function action() {

        // 基本情報を渡す
        $objSiteInfo = SC_Helper_DB_Ex::sfGetBasisData();
        $this->arrInfo = $objSiteInfo->data;

        //おすすめ商品表示
        $this->arrBestProducts = $this->lfGetRanking();

        //商品ステータス取得
        $this->productStatus = $this->lfGetProductStatus($this->arrBestProducts);
    }

    /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }
    /**
     * おすすめ商品検索.
     *
     * @return array $arrBestProducts 検索結果配列
     */
    function lfGetRanking() {
        $objQuery =& SC_Query_Ex::getSingletonInstance();
        $objProduct = new SC_Product_Ex();

        // おすすめ商品取得
        $col = 'T1.best_id, T1.category_id, T1.rank, T1.product_id, T1.title, T1.comment, T1.create_date, T1.update_date';
        $table = 'dtb_best_products as T1 INNER JOIN dtb_products as T2 ON T1.product_id = T2.product_id';
        $where = 'T1.del_flg = 0 and T2.status = 1';
        $objQuery->setOrder('T1.rank');
        $objQuery->setLimit(RECOMMEND_NUM);
        $arrBestProducts = $objQuery->select($col, $table, $where);

        $objQuery =& SC_Query_Ex::getSingletonInstance();
        if (count($arrBestProducts) > 0) {
            // 商品一覧を取得
            // where条件生成&セット
            $arrProductId = array();
            $where = 'product_id IN (';
            foreach ($arrBestProducts as $key => $val) {
                $arrProductId[] = $val['product_id'];
            }
            // 取得
            $arrProductList = $objProduct->getListByProductIds($objQuery, $arrProductId);

            // おすすめ商品情報にマージ
            foreach ($arrBestProducts as $key => $value) {
                $arrRow =& $arrBestProducts[$key];
                if (isset($arrProductList[$arrRow['product_id']])) {
                    $arrRow = array_merge($arrRow, $arrProductList[$arrRow['product_id']]);
                } else {
                    // 削除済み商品は除外
                    unset($arrBestProducts[$key]);
                }
            }

            // 商品一覧データに割引率を追加
            foreach($arrBestProducts as $key => $val){
                if(isset($val['price01_max'])){
                    $arrBestProducts[$key]['drate_max'] = round(100 - $val['price02_max'] / $val['price01_max'] * 100);
                }
                if(isset($val['price01_min'])){
                    $arrBestProducts[$key]['drate_min'] = round(100 - $val['price02_min'] / $val['price01_min'] * 100);
                }
            }
        }
        return $arrBestProducts;
    }
    /**
     * 商品ステータス取得関数
     *
     * @return array $productStatus 商品ステータス配列
     */
    function lfGetProductStatus($arrBestProducts){
        $objQuery =& SC_Query_Ex::getSingletonInstance();
        $col = 'product_status_id';
        $table = 'dtb_product_status';
        $where = 'product_id = ?';
        $arrval = '';
        foreach($arrBestProducts as $val){
            $arrval =  array($val['product_id']);
            $productStatus[$val['product_id']] = $objQuery->getCol($col, $table, $where, $arrval);
        }
        return $productStatus;
    }
}

前回のコードと比較して変わったところといえば、lfGetRankingメソッドに割引率を計算するロジックを入れたくらいですかね。

 

data/Smarty/templates/default/frontparts/bloc/recommend.tpl のコードを以下のように修正(全文掲載)

<!--{*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2012 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.
 *}-->
<!--{if count($arrBestProducts) > 0}-->
    <div class="block_outer clearfix">
        <div id="recommend_area">
            <h2><img src="<!--{$TPL_URLPATH}-->img/title/tit_bloc_recommend.jpg" alt="*" class="title_icon" /></h2>
            <div class="block_body clearfix">
                <!--{foreach from=$arrBestProducts item=arrProduct name="recommend_products"}-->
                    <div class="product_item clearfix">
                        <div class="productImage">
                            <a href="<!--{$smarty.const.P_DETAIL_URLPATH}--><!--{$arrProduct.product_id|u}-->">
                                <img src="<!--{$smarty.const.ROOT_URLPATH}-->resize_image.php?image=<!--{$arrProduct.main_list_image|sfNoImageMainList|h}-->&amp;width=80&amp;height=80" alt="<!--{$arrProduct.name|h}-->" />
                            </a>
                        </div>
                        <div class="productContents">
                            <!--商品ステータス-->
                            <!--{assign var=id value=$arrProduct.product_id}-->
                            <!--{if count($productStatus[$id]) > 0}-->
                                <ul class="status_icon clearfix">
                                <!--{foreach from=$productStatus[$id] item=status}-->
                                    <li>
                                        <img src="<!--{$TPL_URLPATH}--><!--{$arrSTATUS_IMAGE[$status]}-->" alt="<!--{$arrSTATUS[$status]}-->"/>
                                    </li>
                                <!--{/foreach}-->
                                </ul>
                            <!--{/if}-->
                            <h3>
                                <a href="<!--{$smarty.const.P_DETAIL_URLPATH}--><!--{$arrProduct.product_id|u}-->"><!--{$arrProduct.name|h}--></a>
                            </h3>
                            <p class="sale_price">
                                <!--{$smarty.const.NORMAL_PRICE_TITLE}-->(税込): <span class="price"><!--{$arrProduct.price01_min_inctax|number_format}--> 円</span>
                            </p>
                            <p class="sale_price">
                                <!--{$smarty.const.SALE_PRICE_TITLE}-->(税込): <span class="price"><!--{$arrProduct.price02_min_inctax|number_format}--> 円</span>
                            </p>
							<!--★割引率-->
							<!--{if $arrProduct.drate_min != ''}-->
								<div class="pricebox sale_price">
								    割引率:
    								<span class="price">
            						<!--{if $arrProduct.drate_min == $arrProduct.drate_max}-->
                						<!--{$arrProduct.drate_min|sfCalcIncTax:$arrSiteInfo.tax:$arrSiteInfo.tax_rule|number_format}-->%OFF
            						<!--{else}-->
                						<!--{$arrProduct.drate_max|sfCalcIncTax:$arrSiteInfo.tax:$arrSiteInfo.tax_rule|number_format}-->~<!--{$arrProduct.drate_min|sfCalcIncTax:$arrSiteInfo.tax:$arrSiteInfo.tax_rule|number_format}-->%OFF
            						<!--{/if}-->
    								</span>
								</div>
							<!--{/if}-->
                            <p class="mini comment"><!--{$arrProduct.comment|h|nl2br}--></p>
                        </div>
                    </div>
                    <!--{if $smarty.foreach.recommend_products.iteration % 2 === 0}-->
                        <div class="clear"></div>
                    <!--{/if}-->
                <!--{/foreach}-->
            </div>
        </div>
    </div>
<!--{/if}-->

通常価格と割引率を表示させる記述が増えたくらいです。

コメントを残す

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

* Copy This Password *

* Type Or Paste Password Here *

*

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