バージョン選択

フォーラム

メニュー

オンライン状況

64 人のユーザが現在オンラインです。 (60 人のユーザが フォーラム を参照しています。)
登録ユーザ: 0
ゲスト: 64
もっと...

サイト内検索

質問 > フロント機能 > 商品詳細ページの関連カテゴリの親カテゴリ子カテゴリの関連付け【連投申し訳ありません。】

フロント機能

新規スレッドを追加する

スレッド表示 | 新しいものから 前のトピック | 次のトピック | 下へ
投稿者 スレッド
densuke
投稿日時: 2018/8/14 12:19
対応状況: 解決済
半人前
登録日: 2018/7/26
居住地:
投稿: 29
商品詳細ページの関連カテゴリの親カテゴリ子カテゴリの関連付け【連投申し訳ありません。】
▼テンプレート
[EC-CUBE] 3.0.16

連投申し訳ありません。

商品詳細ページの関連カテゴリ部分についてです。


親カテゴリ>子カテゴリ>孫カテゴリ


上記のように表示されていますが、親カテゴリがID=1の時に、
子カテゴリor孫カテゴリの情報を取り出したいです。


{% for ProductCategory in Product.ProductCategories %} 
 {% if ProductCategory.Category.id == 1 %} 
  {% for Category in ProductCategory.Category.path %} 
   {% for Category in Category.Children %}] 
    {{ dump(Category) }} 
   {% endfor %}
  {% endfor %}
 {% endif %}
{% endfor %}


上の書き方では親カテゴリの子カテゴリが全て表示されてしまいます。
せめて該当するカテゴリをどうやって判断しているのか分かればいいのですが、dump()で見ても該当しないカテゴリとの違いが分かりません。

よろしくお願いします。
hmorita_j
投稿日時: 2018/8/14 18:38
対応状況: −−−
長老
登録日: 2017/3/3
居住地: 沈黙の巨大都市松戸
投稿: 222
Re: 商品詳細ページの関連カテゴリの親カテゴリ子カテゴリの関連付け【連投申し訳ありません。】
商品のカテゴリのうち、親カテゴリがID=1のもののみ、

親カテゴリ>子カテゴリ>孫カテゴリ

と表示したい、ということでしょうか?


{% for ProductCategory in Product.ProductCategories %} 
    {% if ProductCategory.Category.path[0].id == 1 %}
        {% for Category in ProductCategory.Category.path %} 
            {{ dump(Category) }} 
        {% endfor %}
    {% endif %}
{% endfor %}


これでどうでしょうか?
densuke
投稿日時: 2018/8/14 23:15
対応状況: −−−
半人前
登録日: 2018/7/26
居住地:
投稿: 29
Re: 商品詳細ページの関連カテゴリの親カテゴリ子カテゴリの関連付け【連投申し訳ありません。】
ご返信ありがとうございます。

説明不足でした、子カテゴリor孫カテゴリのみを抽出したいということです。

自己解決致しましたので、コードを載せておきますが、idを指定しているのでコード的には微妙です。
他にやりようはありそうです。

同じ様なことを実現したい人は参考までに。
twigのパターンとControllerのパターンを載せますが、
twigのコードは最下層IDを取得するだけなので、controllerの方が凡庸性高いです。


商品詳細ページで該当商品の関連カテゴリを表示したい時などに使えます。

【Twigで指定した親カテゴリIDの最下層のカテゴリIDを取得するパターン】

{% for Product in Product.ProductCategories %}
 {% if Product.Category.path.0.id == 1 %}
  {% if loop.last %}
   {% for Category in Product.Category.path %}
    {% if loop.last %}
     {{ dump(Category.id) }}
    {% endif %}
   {% endfor %}
  {% endif %}
 {% endif %}
{% endfor %}


【Controllerで指定した親カテゴリIDの最下層のカテゴリIDを修正するパターン】

$id = $app['request']->get('id');
$Product = $app['eccube.repository.product']->get($id);

$ProductCategory = $Product['ProductCategories'];
$cnt1 = 1;
$cnt2 = 1;
$length1 = count($ProductCategory);
$length2 = array();
 foreach ($ProductCategory as $Product) {
  ++$cnt1;
  if ($Product['Category']['path'][0]['id'] == 790) {
   if ($cnt1 > $length1) {
    $length2 = count($Product['Category']['path']);
     foreach ($Product['Category']['path'] as $Category) {
      ++$cnt2; if ($cnt2 > $length2) {
      $CategoryId = $Category['id'];
     }
   }
  }
 }
}
スレッド表示 | 新しいものから 前のトピック | 次のトピック | トップ


 



ログイン


EC-CUBE公式 Amazon Payプラグイン

統計情報

総メンバー数は88,712名です
総投稿数は109,949件です

投稿数ランキング

1
seasoft
7367
2
468
3217
3
AMUAMU
2712
4
nanasess
2313
5
umebius
2085
6
yuh
1819
7
h_tanaka
1638
8
red
1570
9
mcontact
1285
10
tsuji
958
11
fukap
907
12
shutta
835
13
tao_s
799
14 ramrun 789
15 karin 689
16 sumida 641
17
homan
633
18 DELIGHT 572
19
patapata
502
20
flealog
485


ネットショップの壺

EC-CUBEインテグレートパートナー

Copyright© EC-CUBE CO.,LTD. All Rights Reserved.