[EC-CUBE]商品詳細ページで在庫数を別ウィンドウで表示する

「[EC-CUBE]商品詳細ページで在庫数を別ウィンドウで表示する」の編集履歴(バックアップ)一覧に戻る

[EC-CUBE]商品詳細ページで在庫数を別ウィンドウで表示する - (2009/04/21 (火) 16:08:32) の編集履歴(バックアップ)


EC-CUBEの商品詳細ページに在庫数を表示する方法(別ウィンドウ表示編)

【用意するもの】
  • EC-CUBE2がインストールされた状態のサーバ
  • EC-CUBEカスタマイズするための予備知識
(参照:レシピ2 EC-CUBEカスタマイズ、ちょっとその前に・・・ファイルの位置関係を把握しよう。)


皆さん、こんにちは!家庭科担当の穂満です。
今日もざっくざっくとEC-CUBEを料理していきましょう!

さて今回は、EC-CUBE無い機能で、あったら便利な機能の一つだと勝手に思っている「在庫数表示機能」の追加です。
また、在庫表示にも別ウィンドウに表示する方法と詳細ページそのまま表示する方法の2パターンがありますが、今回は前者の「別ウィンドウに表示する方法」をご説明します。
別ウィンドウに表示するメリットは、詳細ページのデザインを崩したくないときに有効です。というのも、規格名が長かったりすると詳細ページのデザインが崩れる恐れがあるからです。

それでは、早速見ていきましょう!


調理例
下図のような感じになります。
詳細ページに「在庫の詳細を見る」というリンクを張り、別ウィンドウ内に表示します。

在庫数表示の調理例




どういう作業が必要か
始める前に、まずどういった作業をしていくのか把握しておきましょう。
在庫数表示はもともと無い機能ですから、在庫数をデータベースから収集するPHPスクリプトと内容を表示するためのデザインテンプレートが必要になります。
そして、詳細ページから別ウィンドウに開く為のリンクを張るので、詳細ページのデザインテンプレートを修正してリンクを張らねばなりません。

まとめますと、以下の3ステップになります。

  1. 在庫表示用PHPスクリプトの作成
  2. 在庫表示用テンプレートの作成
  3. 詳細ページからリンクを張る




(2ページへ続く)

(1) 在庫表示用PHPスクリプトの作成
さて、まず最初に在庫状況をデータベースから取得してテンプレート側に情報を渡すPHPスクリプトを作成します。在庫数なので、一連のファイル名には「stock(ストック)」というキーワードをファイル名やクラス名に利用することにします。

この場合気をつけなければならないのが、3つの新しいPHPスクリプトを作成する必要があるところです。その3つとは、

   * /html/products/stock.php
   * /data/class_extends/page_extends/products/LC_Page_Products_Stock_Ex.php
   * /data/class/pages/products/LC_Page_Products_Stock.php

になります。なんだか面倒そうな感じですが、決して難しくないので下記の各ファイルの役割を理解しながら、ファイルを作成して下さい。


● /html/products/stock.php ・・・ 単なる窓口(インターフェース)の役割です。内部で「LC_Page_Products_Stock_Ex.php」を呼び出しています。

/html/products/stock.php

<?php

/*

* This file is part of EC-CUBE

*

* Copyright(c) 2000-2007 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.

*/




require_once("../require.php");

require_once(CLASS_EX_PATH . "page_extends/products/LC_Page_Products_Stock_Ex.php");







$objPage = new LC_Page_Products_Stock_Ex();

register_shutdown_function(array($objPage, "destroy"));

$objPage->init();

$objPage->process();

?>





● LC_Page_Products_Stock_Ex.php ・・・ 「LC_Page_Products_Stock.php」を内部で呼び出しています。もし、「LC_Page_Products_Stock.php」で修正したい内容や追記したい内容がある場合はこのファイルに書き込みます。「LC_Page_Products_Stock.php」内で既に定義済みである関数名と同じ名前の関数をこちらのファイルに新しく定義すると、こちらの方が優先(継承)されます。この辺のお話は「レシピ2 EC-CUBEカスタマイズ、ちょっとその前に・・・ファイルの位置関係を把握しよう。」の3ページをご参照下さい。

/data/class_extends/page_extends/products/LC_Page_Products_Stock_Ex.php

<?php

/*

* This file is part of EC-CUBE

*

* Copyright(c) 2000-2007 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.

*/




require_once(CLASS_PATH . "pages/products/LC_Page_Products_Stock.php");



/**

* LC_Page_Products_Stock のページクラス(拡張).

*

* LC_Page_Products_Stock をカスタマイズする場合はこのクラスを編集する.

*

* @package Page

* @author ARATANA INC.

* @version $Id: LC_Page_Products_Stock_Ex.php 2008-05-13 22:41:55Z $

*/

class LC_Page_Products_Stock_Ex extends LC_Page_Products_Stock {



   // }}}

   // {{{ functions



   /**

    * Page を初期化する.

    *

    * @return void

    */

   function init() {

       parent::init();

   }



   /**

    * Page のプロセス.

    *

    * @return void

    */

   function process() {

       parent::process();

   }



   /**

    * デストラクタ.

    *

    * @return void

    */

   function destroy() {

       parent::destroy();

   }

}

?>




● LC_Page_Products_Stock.php ・・・ 「LC_Page_Products_Stock_Ex.php」と大変名前が似ていますが、これがメインのスクリプトになります。

/data/class/pages/products/LC_Page_Products_Stock.php

<?php

/*

* This file is part of EC-CUBE

*

* Copyright(c) 2000-2007 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.

*/




require_once(CLASS_PATH . "pages/LC_Page.php");



/**

* 在庫表示 のページクラス.

*

* @package Page

* @author ARATANA INC.

* @version $Id: LC_Page_Products_Stock.php 2008-05-13 22:32:27Z $

*/

class LC_Page_Products_Stock extends LC_Page {



   // }}}

   // {{{ functions



   /**

    * Page を初期化する.

    *

    * @return void

    */

   function init() {

       parent::init();

       $this->tpl_mainpage = 'products/stock.tpl';

   }



   /**

    * Page のプロセス.

    *

    * @return void

    */

   function process() {

       $objView = new SC_SiteView();

       $objDb = new SC_Helper_DB_Ex();



       // 管理ページからの確認の場合は、非公開の商品も表示する。

       if(isset($_GET['admim']) && $_GET['admin'] == 'on') {

           SC_Utils_Ex::sfIsSuccess(new SC_Session());

           $where = "del_flg = 0";

       } else {

           $where = "del_flg = 0 AND status = 1";

       }



       // 値の正当性チェック

       if(!SC_Utils_Ex::sfIsInt($_GET['product_id']) || !$objDb->sfIsRecord("dtb_products", "product_id", $_GET['product_id'], $where)) {

           SC_Utils_Ex::sfDispSiteError(PRODUCT_NOT_FOUND);

       }



       $objQuery = new SC_Query();



       // 商品情報

       $col  = "t1.name, t2.product_code, t2.stock, t2.stock_unlimited, t2.price01, t2.price02, t2.classcategory_id1, t2.classcategory_id2, ";

       $col .= "(SELECT name FROM dtb_classcategory WHERE classcategory_id = t2.classcategory_id1) AS classcategory1_name, (SELECT name FROM dtb_classcategory WHERE classcategory_id = t2.classcategory_id2) AS classcategory2_name, ";

       $col .= "(SELECT rank FROM dtb_classcategory WHERE classcategory_id = t2.classcategory_id1) AS classcategory1_rank, (SELECT rank FROM dtb_classcategory WHERE classcategory_id = t2.classcategory_id2) AS classcategory2_rank";

       $arrRet = $objQuery->select($col, "dtb_products AS t1, dtb_products_class AS t2", "t1.product_id = ? AND t1.product_id = t2.product_id ORDER BY classcategory1_rank DESC, classcategory2_rank DESC", array($_GET['product_id']));



       // 選択している規格を抽出

       $col = "class_id";

       $arrSelectClass = $objQuery->select($col, "dtb_classcategory", "classcategory_id = ".$arrRet[0]['classcategory_id1']." OR classcategory_id = ".$arrRet[0]['classcategory_id2']);



       $where = "";

       if($arrSelectClass[1]['class_id']) {

           $where = "class_id = '".$arrSelectClass[0]['class_id']."' OR class_id = ".$arrSelectClass[1]['class_id']. " ORDER BY rank DESC";
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。