EC-CUBE 最近見た商品を実装する(2)

■最近見た商品を実装する(改良Ver)

前回の最近見た商品を実装する(1)のままでは
既に閲覧履歴に入っている商品を再度見た時に繰り上げられない不具合があるので
クッキーに保管された商品を繰り上げする必要がある

■実装手順

定義ファイルにて下記を追記
/html/define.php

define('UNSET_COOKIE_TICKET', time()-180);  // クッキーの解除

クッキーに商品IDを設定する処理を修正する
/data/class_extends/page_extends/products/LC_Page_Products_Detail_Ex.php

  /**
   * 閲覧履歴を設定(Cookie利用)
   */
  function setItemHistory($__id)
  {
    
    $obj_product = new SC_Product_Ex();
    $data = $obj_product->getDetail($__id);
    
    if(is_null($data['product_id']) !== true)
    {
      // 商品詳細ページが存在する場合はクッキーに商品IDを保管する
      $history = array();
      $cookie_count = count($_COOKIE['history']);
      
      if(is_null($_COOKIE['history']) !== true)
      {
        $history = $_COOKIE['history'];
      }
      
      
      if(in_array($data['product_id'], $history) === false)
      {
        if($cookie_count >= PRODUCT_HISTORY_MAX)
        {
          // クッキーの保管限度数を超えたら先頭のクッキーを削除
          array_shift($history);
        }
        
        // 現在閲覧している商品IDををセット
        $history[] = $data['product_id'];
        
        // 
        foreach($history as $key=>$id)
        {
          setcookie('history['.$key.']', $id, COOKIE_TICKET);
        }
      }
      else
      {
        // クッキーに既に登録されている場合は処理しない
        /***** ↓↓↓ 追記 ↓↓↓ *****/
        $key = array_search($data['product_id'], $history);
        unset($history[$key]);
        $history[] = $data['product_id'];
        
        
        // 現在のクッキー情報をクリア
        for($i=0; $i<PRODUCT_HISTORY_MAX; $i++)
        {
          setcookie('history['.$i.']', '', UNSET_COOKIE_TICKET);
        }
        
        foreach($history as $key=>$id)
        {
          setcookie('history['.$key.']', $id, COOKIE_TICKET);
        }
      }
      
    }
    else
    {
      // データ未存在
    }
  }

次に画像がない場合のノーイメージについては『SC_Utils.php』で定義されている『sfNoImageMainList』関数を利用する
独自の画像を使いたい場合は関数を追加するとよい

尚、ブロックの編集を管理画面からではなく直接いじる場合は下記のディレクトリにある
※但し初回ブロック生成時は管理画面から作った方が面倒がない

/data/Smarty/templates/default/frontparts/bloc/itemhistory.tpl

<!--{section name=cnt loop=$arrItemHistory}-->
<div align="center">
  <a href="<!--{$smarty.const.P_DETAIL_URLPATH}--><!--{$arrProduct.product_id|u}-->.php">
    <img src="<!--{$smarty.const.IMAGE_SAVE_URLPATH}--><!--{$arrItemHistory[cnt].main_list_image|sfNoImageMainList|h}-->" alt="<!--{$arrItemHistory[cnt].name|h}-->" class="picture" />
  </a>
  <br />
  <p>
    <a href="<!--{$smarty.const.P_DETAIL_URLPATH}--><!--{$arrProduct.product_id|u}-->.php">
      <!--{$arrItemHistory[cnt].name}-->
    </a>
  </p>
  <p>通常価格:<!--{$arrItemHistory[cnt].price01_min_inctax}--></p>
  <p>販売価格:<!--{$arrItemHistory[cnt].price02_min_inctax}--></p>
</div>
<hr />
<!--{/section}-->

ec_cube_045

Comments are closed.