teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

タグの追加

2019/11/19 10:10

投稿

diz-inc
diz-inc

スコア5

title CHANGED
File without changes
body CHANGED
File without changes

2

誤字修正

2019/11/19 10:10

投稿

diz-inc
diz-inc

スコア5

title CHANGED
File without changes
body CHANGED
@@ -6,9 +6,9 @@
6
6
  上記のサイトを参考にカスタマイズし、特定カテゴリの新着商品を自動で取得、表示することができました。
7
7
 
8
8
  同じ仕様で同ページ内に複数個所、別カテゴリの新着商品を表示させたいのですが、
9
- TwigExtension.php内と新規ブロックtwig内のどこをどのように修正すれば複数設置できますでしょうか?
9
+ TwigExtension.php内と新規ブロックtwig内(ここではnew_item_sec.twig)のどこをどのように修正すれば複数設置できますでしょうか?
10
10
 
11
- 初歩的な質問かと思いますが、よろしくお願いいたします。
11
+ よろしくお願いいたします。
12
12
 
13
13
  ##TwigExtension.php ソースコード
14
14
  ```php

1

ソースコードを載せました

2019/11/19 09:57

投稿

diz-inc
diz-inc

スコア5

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,4 @@
1
+ ##前提・実現したいこと
1
2
  EC-CUBE4.0.3にてTOPページの新着商品ブロックのカスタマイズを行っております。
2
3
 
3
4
  https://www.spreadworks.co.jp/top-new-products-for-ec-cube4/
@@ -7,4 +8,160 @@
7
8
  同じ仕様で同ページ内に複数個所、別カテゴリの新着商品を表示させたいのですが、
8
9
  TwigExtension.php内と新規ブロックtwig内のどこをどのように修正すれば複数設置できますでしょうか?
9
10
 
10
- 初歩的な質問かと思いますが、よろしくお願いいたします。
11
+ 初歩的な質問かと思いますが、よろしくお願いいたします。
12
+
13
+ ##TwigExtension.php ソースコード
14
+ ```php
15
+ <?php
16
+
17
+ namespace Customize\Twig\Extension;
18
+
19
+ use Doctrine\Common\Collections;
20
+ use Doctrine\ORM\EntityManagerInterface;
21
+ use Eccube\Common\EccubeConfig;
22
+ use Eccube\Entity\Master\ProductStatus;
23
+ use Eccube\Entity\Product;
24
+ use Eccube\Entity\ProductClass;
25
+ use Eccube\Repository\ProductRepository;
26
+
27
+ class TwigExtension extends \Twig_Extension
28
+ {
29
+ /** @var EntityManagerInterface */
30
+ private $entityManager;
31
+
32
+ /**
33
+ * @var EccubeConfig
34
+ */
35
+ protected $eccubeConfig;
36
+
37
+ /**
38
+ * @var ProductRepository
39
+ */
40
+ private $productRepository;
41
+
42
+ /**
43
+ * TwigExtension constructor.
44
+ *
45
+ */
46
+ public function __construct(
47
+ EntityManagerInterface $entityManager,
48
+ EccubeConfig $eccubeConfig,
49
+ ProductRepository $productRepository
50
+ ) {
51
+ $this->entityManager = $entityManager;
52
+ $this->eccubeConfig = $eccubeConfig;
53
+ $this->productRepository = $productRepository;
54
+ }
55
+ /**
56
+ * Returns a list of functions to add to the existing list.
57
+ *
58
+ * @return array An array of functions
59
+ */
60
+ public function getFunctions()
61
+ {
62
+ return array(
63
+ new \Twig_SimpleFunction('CustomizeNewProduct', array($this, 'getCustomizeNewProduct')),
64
+ );
65
+ }
66
+
67
+ /**
68
+ * Name of this extension
69
+ *
70
+ * @return string
71
+ */
72
+ public function getName()
73
+ {
74
+ return 'CustomizeTwigExtension';
75
+ }
76
+
77
+ /**
78
+ *
79
+ * 新着商品3件返す
80
+ *
81
+ * @return Products|null
82
+ */
83
+ public function getCustomizeNewProduct()
84
+ {
85
+ try {
86
+ // 既存のproductRepositoryを利用し、商品情報を取得
87
+
88
+
89
+ // 検索条件の新着順を定義
90
+ $searchData = array();
91
+ $qb = $this->entityManager->createQueryBuilder();
92
+ $query = $qb->select("plob")
93
+ ->from("Eccube\Entity\Master\ProductListOrderBy", "plob")
94
+ ->where('plob.id = :id')
95
+ ->setParameter('id', $this->eccubeConfig['eccube_product_order_newer'])
96
+ ->getQuery();
97
+ $searchData['orderby'] = $query->getOneOrNullResult();
98
+
99
+ // 新入荷商品を表示
100
+ $snkr_stock_id = 2;
101
+ $qb = $this->entityManager->createQueryBuilder();
102
+ $query = $qb->select("ctg")
103
+ ->from("Eccube\Entity\Category", "ctg")
104
+ ->where('ctg.id = :id')
105
+ ->setParameter('id', $snkr_stock_id)
106
+ ->getQuery();
107
+ $searchData['category_id'] = $query->getOneOrNullResult();
108
+
109
+ // 商品情報3件取得
110
+ $qb = $this->productRepository->getQueryBuilderBySearchData($searchData);
111
+ $query = $qb->setMaxResults(3)->getQuery();
112
+ $products = $query->getResult();
113
+ return $products;
114
+
115
+ } catch (\Exception $e) {
116
+ return null;
117
+ }
118
+ return null;
119
+ }
120
+ }
121
+
122
+ ```
123
+ ##new_item_sec.twig ソースコード
124
+ ```twig
125
+
126
+ {% set Products = CustomizeNewProduct() %}
127
+ {% if Products|length > 0 %}
128
+
129
+ <div class="ec-role">
130
+ <div class="ec-newItemRole">
131
+
132
+ <div class="ec-secHeading">
133
+ <span class="ec-secHeading__en">{{ 'NEW ARRIVAL'|trans }}</span>
134
+ <span class="ec-secHeading__line"></span>
135
+ <span class="ec-secHeading__ja">{{ '新着商品'|trans }}</span>
136
+ </div>
137
+
138
+ <ul class="top-new-product">
139
+ {% for Product in Products %}
140
+ <li>
141
+ <a href="{{ url('product_detail', {'id': Product.id}) }}">
142
+ <img src="{{ asset(Product.main_list_image|no_image_product, 'save_image') }}">
143
+ <p class="ec-newItemRole__listItemTitle">{{ Product.name }}</p>
144
+ <p class="ec-newItemRole__listItemPrice">
145
+ {% if Product.hasProductClass %}
146
+ {% if Product.getPrice02Min == Product.getPrice02Max %}
147
+ {{ Product.getPrice02IncTaxMin|price }}
148
+ {% else %}
149
+ {{ Product.getPrice02IncTaxMin|price }} ~ {{ Product.getPrice02IncTaxMax|price }}
150
+ {% endif %}
151
+ {% else %}
152
+ {{ Product.getPrice02IncTaxMin|price }}
153
+ {% endif %}
154
+ </p>
155
+ </a>
156
+ </li>
157
+ {% endfor %}
158
+ </ul>
159
+ <div class="view-more-box">
160
+ <a class="ec-inlineBtn--top" href="{{ url('product_list') }}?category_id=7">VEW MORE</a>
161
+ </div>
162
+ </div>
163
+ </div>
164
+ </div>
165
+ {% endif %}
166
+
167
+ ```