LoginSignup
4

More than 5 years have passed since last update.

EC-CUBE3 新商品取得方法

Last updated at Posted at 2016-11-24

最近はEC-CUBE3を触り始めており、かなり試行錯誤しながらやっていますので、苦労の足跡を残していこうと思います。

EC-CUBE3をデフォルトでインストールすると画面中央に新商品の表示がありますが、テンプレートファイル(あなたのルートディレクトリ/src/Eccube/Resource/template/default/Block/new_product.twig)をみるとこいつらは静的データです。

こちらのサイト(http://ohtacky.blogspot.jp/2015/07/eccube3.html)
を参考に"あなたのルートディレクトリ/app/template/default/Block/new_product.twig"と"あなたのルートディレクトリ/src/Eccube/Controller/Block/NewProductController.php"を作成します。

NewProductController.php
public function index(Application $app) {
        // del_flg(削除フラグ)がたっていないものを取得
        $product_data = $app['eccube.repository.product']->findBy(array('del_flg' => 0));
        // unsetするため一時的な変数へ
        $temp = $product_data;
        foreach ($temp as $key => $p) {
            $delete_flg = true;
            // 非公開のものは除外
            if (! $p->isEnable()) { unset($product_data[$key]); continue; }
            // タグが新商品のものを抽出
            $tag = $p->getProductTag();
            foreach ($tag as $k => $val) { if ($val->getTag() == '新商品') $delete_flg = false;  }
            if ($delete_flg) unset($product_data[$key]);
        }
        // テンプレートへ値を渡して出力させる
        return $app['view']->render('Block/new_product.twig', array('NewProduct' => $product_data));
}

delete_flgなんて変なもの使わなくてもいけそうな気がしなくもない。

コントローラをこんな感じで書いたら次はテンプレート

new_product.twig
    <div class="row new-product">
        {% for p in NewProduct | slice(0, 3) %}
        <div class="col-sm-3 col-xs-6">
            <div class="pickup_item">
                <a href="{{  url('product_detail', {id : p.getId()} ) }}">
                    <div class="item_photo"><img src="{{ app.config.image_save_urlpath }}/{{p.getMainListImage()}}"></div>
                    <p class="item_comment text-warning">{{p.note}}</p>
                    <dl>
                        <dt class="item_name">{{p.name}}</dt>
                        <dd class="item_price">¥{{p.getPrice02IncTaxMax()|number_format}}</dd>
                    </dl>
                </a>
            </div>
        </div>
        {% endfor %}
    </div>

twig慣れるのに時間かかりそーーー!

最後に忘れちゃいけないのがDBのdtb_blockテーブルのlogin_flgを1にしておくこと!

私はこれを忘れて半日悩んでしまいました...

これだけ書くのに結構時間をかけてしまいました...
EC-CUBE3の情報は結構少ないのでどんどん書きためていきたいと思います。

$tag = $p->getProductTag();
foreach ($tag as $k => $val) { if ($val->getTag() == '新商品') $delete_flg = false;  }

ここの'新商品'っていう文字列と比較してるのが気持ち悪くてやめたいけど、他にいい方法が思いつかない...

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4