カテゴリー
EC-CUBE osCommerce PHP

EC-CUBEのDBに商品情報のカラムを追加して商品詳細で表示する実験

はい、正直こんなコトやると禿げますヨ。ええ。

EC-CUBEの商品情報に任意の項目を追加して、それを管理ページ「商品管理」の「商品登録」で入力できたり編集できる様にした上で、「商品登録CSV」からCSVファイルアップロードで登録できる様に目論みます。

今回の環境
EC-CUBE 2.12.6が正常に動いてる環境でDBはMySQL使用。

既にヤっちまった部分もあって何か抜けてる可能性もあるので、もし自分もヤってみようと思う方がいれば参考程度にとどめてください。
実際、もーどこイジったか覚えてないデスw

細けー話はご自分で色々調べてもらうとして、早速DBにカラムを追加します。

まずEC-CUBEで使ってるDBにphpMyAdminとかのお手軽ツールで「dtb_products」ってテーブルにサンプルテキスト項目として「sampl_txt1」ってカラムを追加します。

カラム名:sampl_txt1
データ型:text
デフォルト値:NULL

みたいな感じで。
MySQLコマンドで追加する場合は以下みたいな・・・
まずMySQLに入ります。

$ mysql -u MySQLユーザー名 -D DB名 -p
Enter password:

Wekcomeメッセージとか表示されたあとプロンプトが「mysql>」に変ります

今回カラムを追加するテーブル「dtb_products」の確認
desc テーブル名;

mysql> desc dtb_products;
+-------------------+-------------+------+-----+---------------------+-------+
| Field             | Type        | Null | Key | Default             | Extra |
+-------------------+-------------+------+-----+---------------------+-------+
| product_id        | int(11)     | NO   | PRI | NULL                |       |
| name              | text        | NO   |     | NULL                |       |
| maker_id          | int(11)     | YES  |     | NULL                |       |
| status            | smallint(6) | NO   |     | 2                   |       |
| comment1          | text        | YES  |     | NULL                |       |
| comment2          | text        | YES  |     | NULL                |       |
| comment3          | mediumtext  | YES  |     | NULL                |       |
| comment4          | text        | YES  |     | NULL                |       |
| comment5          | text        | YES  |     | NULL                |       |
| comment6          | text        | YES  |     | NULL                |       |
| note              | text        | YES  |     | NULL                |       |
| main_list_comment | text        | YES  |     | NULL                |       |
| main_list_image   | text        | YES  |     | NULL                |       |
| main_comment      | mediumtext  | YES  |     | NULL                |       |
| main_image        | text        | YES  |     | NULL                |       |
| main_large_image  | text        | YES  |     | NULL                |       |
| sub_title1        | text        | YES  |     | NULL                |       |
| sub_comment1      | mediumtext  | YES  |     | NULL                |       |
| sub_image1        | text        | YES  |     | NULL                |       |
| sub_large_image1  | text        | YES  |     | NULL                |       |
| sub_title2        | text        | YES  |     | NULL                |       |
| sub_comment2      | mediumtext  | YES  |     | NULL                |       |
| sub_image2        | text        | YES  |     | NULL                |       |
| sub_large_image2  | text        | YES  |     | NULL                |       |
| sub_title3        | text        | YES  |     | NULL                |       |
| sub_comment3      | mediumtext  | YES  |     | NULL                |       |
| sub_image3        | text        | YES  |     | NULL                |       |
| sub_large_image3  | text        | YES  |     | NULL                |       |
| sub_title4        | text        | YES  |     | NULL                |       |
| sub_comment4      | mediumtext  | YES  |     | NULL                |       |
| sub_image4        | text        | YES  |     | NULL                |       |
| sub_large_image4  | text        | YES  |     | NULL                |       |
| sub_title5        | text        | YES  |     | NULL                |       |
| sub_comment5      | mediumtext  | YES  |     | NULL                |       |
| sub_image5        | text        | YES  |     | NULL                |       |
| sub_large_image5  | text        | YES  |     | NULL                |       |
| sub_title6        | text        | YES  |     | NULL                |       |
| sub_comment6      | mediumtext  | YES  |     | NULL                |       |
| sub_image6        | text        | YES  |     | NULL                |       |
| sub_large_image6  | text        | YES  |     | NULL                |       |
| del_flg           | smallint(6) | NO   |     | 0                   |       |
| creator_id        | int(11)     | NO   |     | NULL                |       |
| create_date       | timestamp   | NO   |     | CURRENT_TIMESTAMP   |       |
| update_date       | timestamp   | NO   |     | 0000-00-00 00:00:00 |       |
| deliv_date_id     | int(11)     | YES  |     | NULL                |       |
+-------------------+-------------+------+-----+---------------------+-------+
45 rows in set (0.00 sec)

こんな感じ。
で、ここに「sampl_txt1」を追加します。
alter table テーブル名 add 追加するカラム名 データ型;

mysql> alter table dtb_products add sampl_txt1 TEXT;
Query OK, 3 rows affected (0.36 sec)
Records: 3  Duplicates: 0  Warnings: 0

で追加完了。
もっかいテーブルのチェック。

mysql> desc dtb_products;
+-------------------+-------------+------+-----+---------------------+-------+
| Field             | Type        | Null | Key | Default             | Extra |
+-------------------+-------------+------+-----+---------------------+-------+
| product_id        | int(11)     | NO   | PRI | NULL                |       |
| name              | text        | NO   |     | NULL                |       |
| maker_id          | int(11)     | YES  |     | NULL                |       |
| status            | smallint(6) | NO   |     | 2                   |       |
| comment1          | text        | YES  |     | NULL                |       |
| comment2          | text        | YES  |     | NULL                |       |
| comment3          | mediumtext  | YES  |     | NULL                |       |
| comment4          | text        | YES  |     | NULL                |       |
| comment5          | text        | YES  |     | NULL                |       |
| comment6          | text        | YES  |     | NULL                |       |
| note              | text        | YES  |     | NULL                |       |
| main_list_comment | text        | YES  |     | NULL                |       |
| main_list_image   | text        | YES  |     | NULL                |       |
| main_comment      | mediumtext  | YES  |     | NULL                |       |
| main_image        | text        | YES  |     | NULL                |       |
| main_large_image  | text        | YES  |     | NULL                |       |
| sub_title1        | text        | YES  |     | NULL                |       |
| sub_comment1      | mediumtext  | YES  |     | NULL                |       |
| sub_image1        | text        | YES  |     | NULL                |       |
| sub_large_image1  | text        | YES  |     | NULL                |       |
| sub_title2        | text        | YES  |     | NULL                |       |
| sub_comment2      | mediumtext  | YES  |     | NULL                |       |
| sub_image2        | text        | YES  |     | NULL                |       |
| sub_large_image2  | text        | YES  |     | NULL                |       |
| sub_title3        | text        | YES  |     | NULL                |       |
| sub_comment3      | mediumtext  | YES  |     | NULL                |       |
| sub_image3        | text        | YES  |     | NULL                |       |
| sub_large_image3  | text        | YES  |     | NULL                |       |
| sub_title4        | text        | YES  |     | NULL                |       |
| sub_comment4      | mediumtext  | YES  |     | NULL                |       |
| sub_image4        | text        | YES  |     | NULL                |       |
| sub_large_image4  | text        | YES  |     | NULL                |       |
| sub_title5        | text        | YES  |     | NULL                |       |
| sub_comment5      | mediumtext  | YES  |     | NULL                |       |
| sub_image5        | text        | YES  |     | NULL                |       |
| sub_large_image5  | text        | YES  |     | NULL                |       |
| sub_title6        | text        | YES  |     | NULL                |       |
| sub_comment6      | mediumtext  | YES  |     | NULL                |       |
| sub_image6        | text        | YES  |     | NULL                |       |
| sub_large_image6  | text        | YES  |     | NULL                |       |
| del_flg           | smallint(6) | NO   |     | 0                   |       |
| creator_id        | int(11)     | NO   |     | NULL                |       |
| create_date       | timestamp   | NO   |     | CURRENT_TIMESTAMP   |       |
| update_date       | timestamp   | NO   |     | 0000-00-00 00:00:00 |       |
| deliv_date_id     | int(11)     | YES  |     | NULL                |       |
| sampl_txt1        | text        | YES  |     | NULL                |       |
+-------------------+-------------+------+-----+---------------------+-------+
46 rows in set (0.01 sec)

ト、追加したカラムも表示されてます。
あとはMySQLから抜けます。

mysql> exit
Bye

こんな感じ。
カラムの位置とかも指定できたり削除したりできるけど、それはMySQLのコマンドとか別で調べてくだちw コマンドは面倒っぽいけど基本操作は結構単純ですね。

で、肝心の「sampl_txt1」カラムに文字列を登録する訳ですが、sqlで直接入力はあまりにもアレなので管理ページの「商品管理」から弄れるようにします。

/data/class/pages/admin/products/LC_Page_Admin_Products_Product.phpの

/**
* パラメーター情報の初期化
*
* @param object $objFormParam SC_FormParamインスタンス
* @param array $arrPost $_POSTデータ
* @return void
*/
function lfInitFormParam(&$objFormParam, $arrPost) {

で括られた部分の

$objFormParam->addParam('temp_main_large_image', 'temp_main_large_image', '', '', array());

の次の行辺に$objFormParamを追加

$objFormParam->addParam('temp_main_large_image', 'temp_main_large_image', '', '', array());
// サンプル TEXT 追加
$objFormParam->addParam('サンプル TEXT 1', 'samp_txt1', STEXT_LEN, 'KVa', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));

みたいな。
で、同じファイル内の

* DBに商品データを登録する
*
* @param object $objUpFile SC_UploadFileインスタンス
* @param object $objDownFile SC_UploadFileインスタンス
* @param array $arrList フォーム入力パラメーター配列
* @return integer 登録商品ID
*/
function lfRegistProduct(&$objUpFile, &$objDownFile, $arrList) {

で括られた中の

$checkArray = array('name', 'status',
'main_list_comment', 'main_comment',
'deliv_fee', 'comment1', 'comment2', 'comment3',
'comment4', 'comment5', 'comment6',
'sale_limit', 'deliv_date_id', 'maker_id', 'note');

ってArrayにカラム「sampl_txt1」を追加する。

$checkArray = array('name', 'status',
'main_list_comment', 'main_comment',
'deliv_fee', 'comment1', 'comment2', 'comment3',
'comment4', 'comment5', 'comment6',
'sale_limit', 'deliv_date_id', 'maker_id', 'note', 'sampl_txt1'); //sampl_txt1追加

こんな感じ。
さらにその下にある

// INSERTする値を作成する。
$sqlval['name'] = $arrList['name'];
$sqlval['status'] = $arrList['status'];
$sqlval['main_list_comment'] = $arrList['main_list_comment'];
$sqlval['main_comment'] = $arrList['main_comment'];
$sqlval['comment1'] = $arrList['comment1'];
$sqlval['comment2'] = $arrList['comment2'];
$sqlval['comment3'] = $arrList['comment3'];
$sqlval['comment4'] = $arrList['comment4'];
$sqlval['comment5'] = $arrList['comment5'];
$sqlval['comment6'] = $arrList['comment6'];

にもカラム「sampl_txt1」を追加する。

// INSERTする値を作成する。
$sqlval['name'] = $arrList['name'];
$sqlval['status'] = $arrList['status'];
$sqlval['main_list_comment'] = $arrList['main_list_comment'];
$sqlval['main_comment'] = $arrList['main_comment'];
$sqlval['comment1'] = $arrList['comment1'];
$sqlval['comment2'] = $arrList['comment2'];
$sqlval['comment3'] = $arrList['comment3'];
$sqlval['comment4'] = $arrList['comment4'];
$sqlval['comment5'] = $arrList['comment5'];
$sqlval['comment6'] = $arrList['comment6'];
//サンプル TEXT 追加
$sqlval['samp_txt1'] = $arrList['samp_txt1'];

これで「商品管理」の「商品登録」に追加カラム「samp_txt1」を管理ページから弄る準備が完了です。多分。
でもこれだけではNGなので、「商品登録」の入力画面や確認画面も弄ります。

まず「商品登録」ページのテンプレート
/data/Smarty/admin/products/product.tplの入力フォーム箇所

<tr>
<th>商品コード<span> *</span></th>
<td>
<span><!--{$arrErr.product_code}--></span>
<input type="text" name="product_code" value="<!--{$arrForm.product_code|h}-->" maxlength="<!--{$smarty.const.STEXT_LEN}-->" style="<!--{if $arrErr.product_code != ""}-->background-color: <!--{$smarty.const.ERR_COLOR}-->;<!--{/if}-->" size="60" />
<span> (上限<!--{$smarty.const.STEXT_LEN}-->文字)</span>
</td>
</tr>

の下に<tr>〜</tr>を追加

<tr>
<th>商品コード<span> *</span></th>
<td>
<span><!--{$arrErr.product_code}--></span>
<input type="text" name="product_code" value="<!--{$arrForm.product_code|h}-->" maxlength="<!--{$smarty.const.STEXT_LEN}-->" style="<!--{if $arrErr.product_code != ""}-->background-color: <!--{$smarty.const.ERR_COLOR}-->;<!--{/if}-->" size="60" />
<span> (上限<!--{$smarty.const.STEXT_LEN}-->文字)</span>
</td>
</tr>
<!-- ▼ サンプルテキスト 追加 -->
<tr>
<th>サンプル TEXT 1</th>
<td>
<span><!--{$arrErr.samp_txt1}--></span>
<input type="text" name="samp_txt1" value="<!--{$arrForm.samp_txt1|h}-->" maxlength="<!--{$smarty.const.STEXT_LEN}-->" size="60" style="<!--{$arrErr.samp_txt1|sfGetErrorColor}-->" />
<span> (上限<!--{$smarty.const.STEXT_LEN}-->文字)</span>
</td>
</tr>
<tr>
<!-- ▲ サンプルテキスト 追加 -->

こんな感じ。
で、「商品登録」の確認ページのテンプレート
/data/Smarty/admin/products/confirm.tplの表示箇所

<tr>
<th>商品コード</th>
<td><!--{$arrForm.product_code|h}--></td>
</tr>

の下に

<tr>
<th>商品コード</th>
<td><!--{$arrForm.product_code|h}--></td>
</tr>
<!-- ▼ サンプルテキスト 追加 -->
<tr>
<th>サンプル TEXT 1</th>
<td><!--{$arrForm.samp_txt1|h}--></td>
</tr>
<!-- ▲ サンプルテキスト 追加 -->

って風に追加。
各表示テンプレートはHTML記述なので任意のレイアウト位置に<tr>〜</tr>を書けばイイと思います。
これで管理ページから登録・編集できるようになります。きっと・・・。

これで商品詳細ページテンプレートの
/data/Smarty/templates/使ってるテンプレート/products/detail.tpl
の任意の箇所に

<!–{$arrProduct.sampl_txt1}–>

って書くと「商品詳細」ページで「sampl_txt1」内の文字列が表示されるハズです。
この辺は「商品詳細」ページのカスタマイズとかを調べてくださいw

追記:ここから

ヒョンなことから商品情報にもー1個カラムを追加することになってこのエントリを見直しながら作業してたら1個書き忘れてましたw

/data/class/SC_Product.php

/**
* 商品詳細の SQL を取得する.
*
* @param string $where_products_class 商品規格情報の WHERE 句
* @return string 商品詳細の SQL
*/
function alldtlSQL($where_products_class = '') {
if (!SC_Utils_Ex::isBlank($where_products_class)) {
$where_products_class = 'AND (' . $where_products_class . ')';
}
/*
* point_rate, deliv_fee は商品規格(dtb_products_class)ごとに保持しているが,
* 商品(dtb_products)ごとの設定なので MAX のみを取得する.
*/
$sql = <<< __EOS__
(
SELECT

のSELECT以下の部分

,dtb_products.create_date
,dtb_products.update_date
,dtb_products.deliv_date_id
,T4.product_code_min
,T4.product_code_max
,T4.price01_min
,T4.price01_max

のあたりにsamp_txt1を追加します。

,dtb_products.create_date
,dtb_products.update_date
,dtb_products.deliv_date_id
,dtb_products.samp_txt1 //サンプル TEXT 追加
,T4.product_code_min
,T4.product_code_max
,T4.price01_min
,T4.price01_max

こんな感じ。

追記:ここまで

ってゆーか何やってんの俺?マジで・・・

そんな感じ。

%d