[EC-CUBE 2.11.4] 特定のカテゴリだけ表示する商品一覧ブロック
以前当サイトで紹介したブロック作成で、特定のカテゴリだけ表示させられないかというご質問が開発コミュニティであったので試しにやってみました。以前の記事はこちら。https://www.nakweb.com/weekly/ec/ec-cube-2-11-4-%e5%95%86%e5%93%81%e4%b8%80%e8%a6%a7%e3%83%96%e3%83%ad%e3%83%83%e3%82%af%e3%82%92%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b/
違うのは、3番目の手順のところだけです。
3.data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Product_List.php を新規作成
<?php
// {{{ requires
require_once CLASS_REALDIR . 'pages/frontparts/bloc/LC_Page_FrontParts_Bloc.php';
/**
* Product_List のページクラス.
*
* @package Page
*/
class LC_Page_FrontParts_Bloc_Product_List extends LC_Page_FrontParts_Bloc {
// }}}
// {{{ functions
/**
* Page を初期化する.
*
* @return void
*/
function init() {
parent::init();
$bloc_file = 'product_list.tpl';
$this->setTplMainpage($bloc_file);
}
/**
* Page のプロセス.
*
* @return void
*/
function process() {
if (defined("MOBILE_SITE") && MOBILE_SITE) {
$objView = new SC_MobileView();
} else {
$objView = new SC_SiteView();
}
$objQuery = new SC_Query_Ex();
// 商品一覧を取得
$col = 'T1.product_id, T1.main_list_image, T1.name, T2.price02 AS price02_min';
$from = 'dtb_products as T1 INNER JOIN dtb_products_class as T2 ON T1.product_id = T2.product_id';
$from .= ' INNER JOIN dtb_product_categories as T3 ON T1.product_id = T3.product_id';
$where = 'T2.del_flg = 0 and T3.category_id IN ( ? , ? )'; // $arrval で指定するカテゴリIDの数だけ ? を増やす
$arrval = Array( 4 , 6 ); // 一番下層のカテゴリIDを指定する
//$objQuery->setOrder("T1.update_date desc");
$arrProducts = $objQuery->select($col, $from, $where, $arrval);
// 重複データ削除
$tmp = Array();
$i = 0;
$max_count = 10; // 取得したい商品個数を指定する
foreach($arrProducts as $arrProduct){
if(!in_array($arrProduct['product_id'], $tmp)){
$this->arrProducts[$i] = $arrProduct;
$i++;
}
$tmp[] = $arrProduct['product_id'];
if($i >= $max_count){
break;
}
}
$objView->assignobj($this);
$objView->display($this->tpl_mainpage);
}
/**
* デストラクタ.
*
* @return void
*/
function destroy() {
parent::destroy();
}
}
?>
コード内にコメントとして記述しましたが、$where のところは、指定するカテゴリ数の分だけ ? を加えてください。
カテゴリ1つなら ( ? ) 、2つなら ( ? , ? )、3つなら ( ? , ? , ? ) って感じです。
$arrval のところは、指定するカテゴリIDをカンマで挟んで記述してください。
価格については端折った処理をしているので、規格分類ごとに価格が異なる場合は特定規格の価格しか表示されないと思いますので注意。
複数カテゴリを指定した場合は、重複して商品が表示されることがあるので、最後に重複データを落としました。
ちょっと冗長だと思いますが、他に良い方法が思いつかなかったのでご容赦。

