[EC-CUBE 2.11.5] 新着商品を表示する
ようやく 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}-->&width=80&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}-->&width=80&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 |

