2015年3月14日土曜日

EC CUBE 2.11系でプラグインを実装する方法

2.12系からプラグインがボタンをクリックするだけで導入できますが、2.11系まではプラグインのインストール機能が無いため、インストールができません。
がしかし、2.11系でもインストールが可能です。

今回は「最近チェックした商品」を導入してみます。

クリックするとプラグインページに飛びます。


ダウンロードして、解凍します。

ブロックの設定
管理画面のデザイン>PC>ブロック設定>ブロックを新規入力で
ブロック名:最近チェックした商品
ファイル名:plg_checkeditems
内容にダウンロードしたプラグインのフォルダ内の「templates」フォルダ内のtplファイル(plg_checkeditems.tpl)をコピペ。

tplファイルとphpファイルの紐付けをデータベースから行います。
データベースへアクセスし、dtb_blocのphp_pathに「frontparts/bloc/plg_checkeditems.php」と記入
プラグインのフォルダ内のplg_checkeditems.phpをECCUBEのフォルダhtml>frontparts>bloc内にコピペ

plg_checkeditems.phpを編集します。
下記行を
require_once PLUGIN_UPLOAD_REALDIR . 'CheckedItems/LC_Page_FrontParts_Bloc_CheckedItems.php';

下記に変更
require_once CLASS_REALDIR . 'pages/frontparts/bloc/LC_Page_FrontParts_Bloc_CheckedItems.php';


プラグインのフォルダ内の「LC_Page_FrontParts_Bloc_CheckedItems.php」をECCUBEフォルダのdata>class>pages>frontparts>bloc内にコピペ
LC_Page_FrontParts_Bloc_CheckedItems.phpを編集します。
下記行を削除
$plugin     = CheckedItems::getPlgInfo();

require_once PLUGIN_UPLOAD_REALDIR . 'CheckedItems/CheckedItems.php';

下記行を
$save_count = $plugin['item_count'];

下記に変更
$save_count = 30 //保存したい件数、今回は30件


下記行を
$arrItem = CheckedItems::getCookieArray();

下記に変更
$arrItem = $_COOKIE['product'];



クッキーに商品へのアクセスを保存する
ECCUBEのフォルダのdata>class>pages>products>LC_Page_Products_Detail.phpを編集
下記ログイン判定の下に追加
        // ログイン判定
        if ($objCustomer->isLoginSuccess() === true) {
            //お気に入りボタン表示
            $this->tpl_login = true;
            $this->is_favorite = SC_Helper_DB_Ex::sfDataExists('dtb_customer_favorite_products', 'customer_id = ? AND product_id = ?', array($objCustomer->getValue('customer_id'), $product_id));
        }

下記の様に追記します
        // ログイン判定
        if ($objCustomer->isLoginSuccess() === true) {
            //お気に入りボタン表示
            $this->tpl_login = true;
            $this->is_favorite = SC_Helper_DB_Ex::sfDataExists('dtb_customer_favorite_products', 'customer_id = ? AND product_id = ?', array($objCustomer->getValue('customer_id'), $product_id));
        }
        
//最近チェックした商品追加
$this->setItemHistory($product_id);

//最近チェックした商品追加

    function setItemHistory($product_id) {

        $cnt = 0;

        // プラグイン情報を取得.
 //       $plugin     = SC_Plugin_Util_Ex::getPluginByPluginCode("CheckedItems");
        //保存期間
        $save_limit = 30;
        //保存件数
        $item_count = 30;

        $arrDisp = $_COOKIE['product'];
        $cnt     = count($arrDisp);

        //重複項目のチェック
        $DispFlg = true;
        if (isset($_COOKIE['product'])) {
            foreach ($_COOKIE['product'] as $name => $value) {
                if($value == $product_id){
                    $DispFlg = false;
                }
            }
        }
 
        //クッキーにセット
        if($DispFlg){
            $disp_num = $item_count;
            if($cnt == 0){
                setcookie('product[' .$cnt .']', $product_id,time()+60*60*24*$save_limit,"/" );
            }else{
                $arrCookie = $_COOKIE['product'];
                $arrCookie[] = $product_id;

                //商品保存処理
                if(count($arrCookie) > $disp_num){
                    array_shift($arrCookie);
                }
                foreach ($arrCookie as $key => $val) {
                    setcookie('product[' .$key .']', $val,time()+60*60*24*$save_limit,"/" );
                }
            }
        }
    }



管理画面のデザイン管理>レイアウト設定から設置したら表示されます。
細かい表記などが表示されていない場合は適時tplファイルを修正。


まとめ
プラグインのブロック追加
DBのブロック情報の変更
プラグイン内のファイルを設置
各ファイルのパスを通す

0 コメント:

コメントを投稿