質問編集履歴

1

PHPファイルの更新、twigの追加

2016/09/05 04:33

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

test CHANGED
@@ -1 +1 @@
1
- EC-CUBE3のプラグイン作成方法
1
+ EC-CUBE3で在庫表示
test CHANGED
@@ -1,81 +1,289 @@
1
- 現在EC-CUBE3でプラグインの開発をしています。
1
+ 現在EC-CUBE3で開発をしています。
2
2
 
3
3
  管理画面の商品マスターで「検索する」を押した時に在庫数を取得し、在庫が0の商品に「在庫切れ」アイコンを表示するということをやりたいです。
4
4
 
5
5
 
6
6
 
7
+
8
+
7
- 管理画面の商品マスタなのでevent.ymlには下記のように記述しました。
9
+ コントロラー下記のように作成しましたが、twigにうまくデータが渡せません
8
-
10
+
9
- ```yml
11
+ ```php
12
+
10
-
13
+ <?php
14
+
15
+ namespace Eccube\Controller\Admin\Product;
16
+
17
+
18
+
11
- admin.product.index.initialize:
19
+ class ProductController extends AbstractController
20
+
12
-
21
+ {
22
+
23
+ public function index(Application $app, Request $request, $page_no = null)
24
+
25
+ {
26
+
27
+
28
+
29
+ $builder = $app['form.factory']
30
+
31
+ ->createBuilder('admin_search_product');
32
+
33
+
34
+
35
+ $event = new EventArgs(
36
+
37
+ array(
38
+
39
+ 'builder' => $builder,
40
+
41
+ ),
42
+
43
+ $request
44
+
45
+ );
46
+
47
+ $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_INITIALIZE, $event);
48
+
49
+
50
+
51
+ $searchForm = $builder->getForm();
52
+
53
+
54
+
55
+ $pagination = array();
56
+
57
+
58
+
59
+ $disps = $app['eccube.repository.master.disp']->findAll();
60
+
61
+ $pageMaxis = $app['eccube.repository.master.page_max']->findAll();
62
+
63
+ $page_count = $app['config']['default_page_count'];
64
+
65
+ $page_status = null;
66
+
67
+ $active = false;
68
+
69
+ $stock = null;
70
+
71
+
72
+
73
+ if ('POST' === $request->getMethod()) {
74
+
75
+
76
+
77
+ $searchForm->handleRequest($request);
78
+
79
+
80
+
81
+ if ($searchForm->isValid()) {
82
+
83
+ $searchData = $searchForm->getData();
84
+
85
+
86
+
87
+ // paginator
88
+
89
+ $qb = $app['eccube.repository.product']->getQueryBuilderBySearchDataForAdmin($searchData);
90
+
91
+ $page_no = 1;
92
+
93
+
94
+
95
+ $event = new EventArgs(
96
+
97
+ array(
98
+
99
+ 'qb' => $qb,
100
+
101
+ 'searchData' => $searchData,
102
+
103
+ ),
104
+
105
+ $request
106
+
107
+ );
108
+
109
+ $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_SEARCH, $event);
110
+
111
+ $searchData = $event->getArgument('searchData');
112
+
113
+
114
+
115
+ $pagination = $app['paginator']()->paginate(
116
+
117
+ $qb,
118
+
119
+ $page_no,
120
+
121
+ $page_count,
122
+
123
+ array('wrap-queries' => true)
124
+
125
+ );
126
+
127
+ // ここから
128
+
129
+ foreach ($pagination as $value) {
130
+
131
+ $Product = $app['eccube.repository.product']->find($value['id']);
132
+
133
+ if (!$Product) {
134
+
135
+ throw new NotFoundHttpException();
136
+
137
+ }
138
+
139
+ $has_class = $Product->hasProductClass();
140
+
141
+
142
+
143
+ if (!$has_class) {
144
+
145
+ $ProductClasses = $Product->getProductClasses();
146
+
13
- - [onAdminProduct1, NORMAL]
147
+ $ProductClass = $ProductClasses[0];
148
+
14
-
149
+ $BaseInfo = $app['eccube.repository.base_info']->get();
150
+
151
+ if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED && $ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) {
152
+
153
+ $ProductClass->setTaxRate($ProductClass->getTaxRule()->getTaxRate());
154
+
155
+ }
156
+
157
+ $ProductStock = $ProductClasses[0]->getProductStock();
158
+
159
+ }
160
+
161
+
162
+
163
+ if ($ProductStock['stock'] === '0'){
164
+
15
- admin.product.index.search:
165
+ $stock[] = '在庫切れ';
166
+
16
-
167
+ } else {
168
+
17
- - [onAdminProduct2, NORMAL]
169
+ $stock[] = '';
170
+
171
+ }
172
+
173
+ }
174
+
175
+ // ここまで実装
176
+
177
+ // sessionのデータ保持
178
+
179
+ $session->set('eccube.admin.product.search', $searchData);
180
+
181
+ }
182
+
183
+ }
184
+
185
+
186
+
187
+ return $app->render('Product/index.twig', array(
188
+
189
+ 'searchForm' => $searchForm->createView(),
190
+
191
+ 'pagination' => $pagination,
192
+
193
+ 'disps' => $disps,
194
+
195
+ 'pageMaxis' => $pageMaxis,
196
+
197
+ 'page_no' => $page_no,
198
+
199
+ 'page_status' => $page_status,
200
+
201
+ 'page_count' => $page_count,
202
+
203
+ 'active' => $active,
204
+
205
+ //'stock' => $stock
206
+
207
+ ));
208
+
209
+ }
18
210
 
19
211
  ```
20
212
 
21
- コントローラーも作成し下記のように作成しましたが、メソッド内にどういう処理を書けばよいかわかりません。
22
-
23
- ```php
24
-
25
- <?php
26
-
27
- namespace Plugin\StockControll;
28
-
29
-
30
-
31
- use Eccube\Application;
32
-
33
- use Eccube\Common\Constant;
34
-
35
- use Eccube\Entity\Category;
36
-
37
- use Eccube\Event\EventArgs;
38
-
39
- use Eccube\Event\TemplateEvent;
40
-
41
- use Plugin\CategoryContent\Entity\CategoryContent;
42
-
43
- use Symfony\Component\Form\FormInterface;
44
-
45
- use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
46
-
47
- use Symfony\Component\DomCrawler\Crawler;
48
-
49
-
50
-
51
- class StockControllEvent
52
-
53
- {
54
-
55
- /** @var \Eccube\Application $app */
56
-
57
- private $app;
58
-
59
-
60
-
61
- public function __construct($app)
62
-
63
- {
64
-
65
- $this->app = $app;
66
-
67
- }
68
-
69
-
70
-
71
- public function onAdminProduct2(EventArgs $event)
72
-
73
- {
74
-
75
- }
76
-
77
- }
213
+ ```twig
214
+
215
+
216
+
217
+ {% for Product in pagination %}
218
+
219
+ <div id="result_list__item--{{ Product.id }}" class="item_box tr">
220
+
221
+ <div id="result_list__id--{{ Product.id }}" class="item_id td">
222
+
223
+ {{Product.id}}
224
+
225
+ </div>
226
+
227
+ <div id="result_list__image--{{ Product.id }}" class="item_photo td">
228
+
229
+ <a href="{{ url('admin_product_product_edit', { id : Product.id }) }}">
230
+
231
+ <img src="{{ app.config.image_save_urlpath }}/{{ Product.mainFileName|no_image_product }}" />
232
+
233
+ </a>
234
+
235
+ </div>
236
+
237
+ <div id="result_list__name--{{ Product.id }}" class="item_detail td">
238
+
239
+ <a href="{{ url('admin_product_product_edit', { id : Product.id }) }}">
240
+
241
+ {{ Product.name }}
242
+
243
+ </a>
244
+
245
+ {{ stock }}
246
+
247
+ <br>
248
+
249
+ <span id="result_list__code--{{ Product.id }}">
250
+
251
+ {{ Product.code_min }}
252
+
253
+ {% if Product.code_min != Product.code_max %} ~ {{ Product.code_max }}
254
+
255
+ {% endif %}
256
+
257
+ </span>
258
+
259
+ </div>
260
+
261
+ <div id="result_list__item_menu_box--{{ Product.id }}"class="icon_edit td">
262
+
263
+ <div id="result_list__item_menu_toggle--{{ Product.id }}" class="dropdown">
264
+
265
+ <a class="dropdown-toggle" data-toggle="dropdown"><svg class="cb cb-ellipsis-h"><use xlink:href="#cb-ellipsis-h"></svg></a>
266
+
267
+ <ul id="result_list__item_menu--{{ Product.id }}" class="dropdown-menu dropdown-menu-right">
268
+
269
+ <li><a href="{{ url('admin_product_product_class', { id : Product.id }) }}">規格</a></li>
270
+
271
+ <li><a href="{{ url('admin_product_product_display', {'id' : Product.id}) }}" target="_blank">確認</a></li>
272
+
273
+ <li><a href="{{ url('admin_product_product_copy', {'id' : Product.id}) }}" {{ csrf_token_for_anchor() }} data-method="post" data-message="商品情報を複製してもよろしいですか?">複製</a></li>
274
+
275
+ <li><a href="{{ url('admin_product_product_delete', {'id' : Product.id}) }}" {{ csrf_token_for_anchor() }} data-method="delete" data-message="商品情報を削除してもよろしいですか?">削除</a></li>
276
+
277
+ </ul>
278
+
279
+ </div>
280
+
281
+ </div>
282
+
283
+ </div><!-- /.item_box -->
284
+
285
+ {% endfor %}
78
286
 
79
287
  ```
80
288
 
81
- PHPの経験はあるのすが、EC-CUBEては全の初心者のためグっもよわかりません。どなたかご教授頂きたいです。よろしくお願いします。
289
+ PHPpagination在庫情報を入れて渡そうとましたがうまいかず、レンダリンする箇所で在庫情報を渡すとArrayと表示されうまいきません。どうすればしょうか?よろしくお願いします。