バージョン選択

フォーラム

メニュー

オンライン状況

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

サイト内検索

質問 > 管理機能 > 特定カテゴリでのおすすめ商品の表示数について

管理機能

新規スレッドを追加する

スレッド表示 | 新しいものから 前のトピック | 次のトピック | 下へ
投稿者 スレッド
r.ts
投稿日時: 2024/3/3 11:32
対応状況: −−−
半人前
登録日: 2024/2/29
居住地:
投稿: 11
特定カテゴリでのおすすめ商品の表示数について
▼テンプレート
[EC-CUBE] EC-CUBE4.1
[レンタルサーバ] xserver
[PHP] 7.4
[データベース] Mysql
[導入プラグインの有無] おすすめ商品プラグイン
[カスタマイズの有無] 無し
[現象]

デフォルトのおすすめ商品プラグインについて
質問です。

下記の方法で特定カテゴリのおすすめ商品を一覧で
表示しています。


{% set recommend_products = repository('Plugin\\Recommend4\\Entity\\RecommendProduct').getRecommendProduct %}

<div class="ec-shelfRole">
    <ul class="ec-shelfGrid">
{% for RecommendProduct in recommend_products %}
{% set ProductCategories = repository('Eccube\\Entity\\ProductCategory').findBy({product_id: RecommendProduct.Product.id}) %}
{% set hasCategory = false %}
{% for ProductCategory in ProductCategories %}
{% if ProductCategory.category_id == 7 %}
{% set hasCategory = true %}
    <li class="ec-shelfGrid__item">
                <a href="{{ url('product_detail', {'id': RecommendProduct.Product.id}) }}">
                    <img src="{{ asset(RecommendProduct.Product.mainFileName|no_image_product, "save_image") }}">
                    <p>{{ RecommendProduct.comment|raw|nl2br }}</p>
                    <dl>
                        <dt class="item_name">{{ RecommendProduct.Product.name }}</dt>
                        <dd class="item_price">
                            {% if RecommendProduct.Product.hasProductClass %}
                                {% if RecommendProduct.Product.getPrice02Min == RecommendProduct.Product.getPrice02Max %}
                                    {{ RecommendProduct.Product.getPrice02IncTaxMin|price }}
                                {% else %}
                                    {{ RecommendProduct.Product.getPrice02IncTaxMin|price }} ~ {{ RecommendProduct.Product.getPrice02IncTaxMax|price }}
                                {% endif %}
                            {% else %}
                                {{ RecommendProduct.Product.getPrice02IncTaxMin|price }}
                            {% endif %}
                        </dd>
                    </dl>
                </a>
            </li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>


ここで表示数4つだけにしたい場合

{% set ProductCategories = repository('Eccube\\Entity\\ProductCategory').findBy({product_id: RecommendProduct.Product.id},4,0) %}


とすると下記のようなエラーが出てしまいます。

Argument 2 passed to Doctrine\ORM\EntityRepository::findBy() must be of the type array or null, int given, called in /httpdocs/ec/vendor/twig/twig/src/Extension/CoreExtension.php on line 1564

どのようにしたらよいでしょうか
k.nakayama
投稿日時: 2024/3/3 12:11
対応状況: −−−
常連
登録日: 2019/10/11
居住地:
投稿: 52
Re: 特定カテゴリでのおすすめ商品の表示数について
findByは2番目の引数は並び順の指定ですが、書かれたコードにはそれがないようです。

https://umebius.com/eccube/how-to-use-repository-findby-method/

並び順を指定しないなら、nullか空配列を渡すかだと思います。

.findBy({product_id: RecommendProduct.Product.id},4,0) ではなく

.findBy({product_id: RecommendProduct.Product.id},{}, 4,0) です。


----------------
----------------
開発公式インテグレートパートナー U-Mebius

r.ts
投稿日時: 2024/3/3 15:18
対応状況: −−−
半人前
登録日: 2024/2/29
居住地:
投稿: 11
Re: 特定カテゴリでのおすすめ商品の表示数について
.findBy({product_id: RecommendProduct.Product.id},{}, 4,0)

だと同様のエラーがでてしまいましたので
下記のようにしてみました。

{% set ProductCategories = repository('Eccube\\Entity\\ProductCategory').findBy({product_id: RecommendProduct.Product.id ,'category_id': 7},{},4,0) %}


エラーは解消されましたが
なぜか全件が表示されてしまうようです。
k.nakayama
投稿日時: 2024/3/3 16:11
対応状況: −−−
常連
登録日: 2019/10/11
居住地:
投稿: 52
Re: 特定カテゴリでのおすすめ商品の表示数について
ロジックの問題ですね。ProductCategoriesの取得数を制限しても、商品数は4つにならないでしょう。

RecommendProductRepositoryに数を指定して商品取得するメソッド作るのが一番早いですが、twigだけでやるなら

{% set i = i + 1 %} のように、何個表示したか自分でカウントして、4個表示したいなら{% if i <= 4 %}のうにしてif文で制御すれば良いかと思います。


(あと、おそらく実現されたいことは特にfindBy使う必要なさそうですね。
RecommendProduct.Product.ProductCategories で十分ですね。)


----------------
----------------
開発公式インテグレートパートナー U-Mebius

r.ts
投稿日時: 2024/3/5 9:11
対応状況: −−−
半人前
登録日: 2024/2/29
居住地:
投稿: 11
Re: 特定カテゴリでのおすすめ商品の表示数について
ありがとうございました。

下記コードで対応しました。

RecommendProductRepository.php

/**
     * Get recommend product by display status of product.
     *
     * @return array
     */
    public function getRecommendProduct()
    {
        $query = $this->createQueryBuilder('rp')
            ->innerJoin('Eccube\Entity\Product', 'p', 'WITH', 'p.id = rp.Product')
	  ->innerJoin('Eccube\Entity\ProductCategory', 'c', 'WITH', 'c.product_id = rp.Product')
            ->where('p.Status = :Disp')
            ->andWhere('rp.visible = true')
	  ->andWhere('c.category_id = 2')
            ->orderBy('rp.sort_no', 'DESC')
	  ->setMaxResults(1)
            ->setParameter('Disp', ProductStatus::DISPLAY_SHOW)
            ->getQuery();

        return $query->getResult();
    }
スレッド表示 | 新しいものから 前のトピック | 次のトピック | トップ


 



ログイン


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

統計情報

総メンバー数は88,885名です
総投稿数は110,000件です

投稿数ランキング

1
seasoft
7367
2
468
3217
3
AMUAMU
2712
4
nanasess
2313
5
umebius
2085
6
yuh
1819
7
h_tanaka
1646
8
red
1570
9
mcontact
1295
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.