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

質問編集履歴

9

PHP整形いたしました。

2018/06/09 11:35

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -84,57 +84,135 @@
84
84
  ※6/9追記です。
85
85
  ProductController.php
86
86
  ```ここに言語を入力
87
- if ($event->getResponse() !== null)
88
- {
89
- return $event->getResponse();
90
- }
91
-
92
- return $app->redirect($app->url('cart'));
93
- }
94
- elseif ($addCartData['mode'] === 'clear-cart')
87
+ public function detail(Application $app, Request $request, $id)
95
88
  {
89
+ $BaseInfo = $app['eccube.repository.base_info']->get();
90
+ if ($BaseInfo->getNostockHidden() === Constant::ENABLED) {
91
+ $app['orm.em']->getFilters()->enable('nostock_hidden');
92
+ }
93
+ /* @var $Product \Eccube\Entity\Product */
94
+ $Product = $app['eccube.repository.product']->get($id);
95
+ if (!$request->getSession()->has('_security_admin') && $Product->getStatus()->getId() !== 1) {
96
+ throw new NotFoundHttpException();
97
+ }
98
+ if (count($Product->getProductClasses()) < 1) {
99
+ throw new NotFoundHttpException();
100
+ }
101
+ /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
102
+ $builder = $app['form.factory']->createNamedBuilder('', 'add_cart', null, array(
103
+ 'product' => $Product,
104
+ 'id_add_product_id' => false,
105
+ ));
106
+ $event = new EventArgs(
107
+ array(
108
+ 'builder' => $builder,
109
+ 'Product' => $Product,
110
+ ),
111
+ $request
112
+ );
113
+ $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE, $event);
114
+ /* @var $form \Symfony\Component\Form\FormInterface */
115
+ $form = $builder->getForm();
116
+ if ($request->getMethod() === 'POST') {
117
+ $form->handleRequest($request);
118
+ if ($form->isValid()) {
119
+ $addCartData = $form->getData();
120
+ if ($addCartData['mode'] === 'add_favorite') {
121
+ if ($app->isGranted('ROLE_USER')) {
122
+ $Customer = $app->user();
123
+ $app['eccube.repository.customer_favorite_product']->addFavorite($Customer, $Product);
124
+ $app['session']->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId());
125
+ $event = new EventArgs(
126
+ array(
127
+ 'form' => $form,
128
+ 'Product' => $Product,
129
+ ),
130
+ $request
131
+ );
132
+ $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_FAVORITE, $event);
133
+ if ($event->getResponse() !== null) {
134
+ return $event->getResponse();
135
+ }
136
+ return $app->redirect($app->url('product_detail', array('id' => $Product->getId())));
137
+ } else {
138
+ // 非会員の場合、ログイン画面を表示
139
+ // ログイン後の画面遷移先を設定
140
+ $app->setLoginTargetPath($app->url('product_detail', array('id' => $Product->getId())));
141
+ $app['session']->getFlashBag()->set('eccube.add.favorite', true);
142
+ return $app->redirect($app->url('mypage_login'));
143
+ }
144
+ } elseif ($addCartData['mode'] === 'add_cart') {
145
+ log_info('カート追加処理開始', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity']));
96
- try
146
+ try {
147
+ $app['eccube.service.cart']->addProduct($addCartData['product_class_id'], $addCartData['quantity'])->save();
148
+ } catch (CartException $e) {
149
+ log_info('カート追加エラー', array($e->getMessage()));
150
+ $app->addRequestError($e->getMessage());
97
- {
151
+ }
152
+ log_info('カート追加処理完了', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity']));
153
+ $event = new EventArgs(
154
+ array(
155
+ 'form' => $form,
156
+ 'Product' => $Product,
157
+ ),
158
+ $request
159
+ );
160
+ $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_COMPLETE, $event);
161
+ if ($event->getResponse() !== null) {
162
+ return $event->getResponse();
163
+ }
164
+ return $app->redirect($app->url('cart'));
165
+ }
166
+ elseif ($addCartData['mode'] === 'clear-cart') {
167
+ try {
98
- $app['eccube.service.cart']->clear()->save();
168
+ $app['eccube.service.cart']->clear()->save();
169
+ } catch (CartException $e) {
170
+ log_info('カートクリアエラー', array($e->getMessage()));
99
- }
171
+ }
100
-
101
- catch(CartException $e)
102
- {
103
- log_info('カートクリアエラー', array(
172
+ return $app->redirect($app->url('product_detail', array('id' => $Product->getId())));
104
- $e->getMessage()
105
- ));
106
- }
173
+ }
107
-
108
- return $app->redirect($app->url('product_detail', array(
109
- 'id' => $Product->getId()
110
- )));
111
- }
174
+ }
175
+ } else {
176
+ $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite');
177
+ if (!empty($addFavorite)) {
178
+ // お気に入り登録時にログインされていない場合、ログイン後にお気に入り追加処理を行う
179
+ if ($app->isGranted('ROLE_USER')) {
180
+ $Customer = $app->user();
181
+ $app['eccube.repository.customer_favorite_product']->addFavorite($Customer, $Product);
182
+ $app['session']->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId());
112
- }
183
+ }
113
- }
184
+ }
114
- else
115
- {
185
+ }
116
- $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite');```
117
186
  ```
118
187
  AddCartType.php
119
188
 
120
189
  ```ここに言語を入力
121
- public
122
-
123
- function validate($data, ExecutionContext $context)
190
+ public function validate($data, ExecutionContext $context)
124
- {
191
+ {
125
- if ($mode !== 'add_favorite' && $mode !== 'clear-cart')
192
+ if ($mode !== 'add_favorite' && $mode !== 'clear-cart') {
126
- {
127
- $context->validateValue($data['product_class_id'], array(
193
+ $context->validateValue($data['product_class_id'], array(
128
- new AssertNotBlank() ,
194
+ new Assert\NotBlank(),
129
- ) , '[product_class_id]');
195
+ ), '[product_class_id]');
130
- if ($this->Product->getClassName1())
196
+ if ($this->Product->getClassName1()) {
131
- {
132
- $context->validateValue($data['classcategory_id1'], array(
197
+ $context->validateValue($data['classcategory_id1'], array(
133
- new AssertNotBlank() ,
198
+ new Assert\NotBlank(),
134
- new AssertNotEqualTo(array(
199
+ new Assert\NotEqualTo(array(
135
- 'value' => '__unselected',
200
+ 'value' => '__unselected',
136
- 'message' => 'form.type.select.notselect'
201
+ 'message' => 'form.type.select.notselect'
137
- )) ,
202
+ )),
138
- ) , '[classcategory_id1]');
203
+ ), '[classcategory_id1]');
139
- }
204
+ }
205
+ //商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
206
+ if ($this->Product->getClassName2()) {
207
+ $context->validateValue($data['classcategory_id2'], array(
208
+ new Assert\NotBlank(),
209
+ new Assert\NotEqualTo(array(
210
+ 'value' => '__unselected',
211
+ 'message' => 'form.type.select.notselect'
212
+ )),
213
+ ), '[classcategory_id2]');
214
+ }
215
+ }
216
+ }
217
+ }
140
218
  ```

8

PHPの整形を致しました。

2018/06/09 11:35

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -114,8 +114,9 @@
114
114
  else
115
115
  {
116
116
  $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite');```
117
+ ```
117
118
  AddCartType.php
118
- ```
119
+
119
120
  ```ここに言語を入力
120
121
  public
121
122
 

7

PHPの整形をしました

2018/06/09 10:47

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -115,6 +115,7 @@
115
115
  {
116
116
  $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite');```
117
117
  AddCartType.php
118
+ ```
118
119
  ```ここに言語を入力
119
120
  public
120
121
 

6

phpの整形をしました

2018/06/09 10:46

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -84,76 +84,55 @@
84
84
  ※6/9追記です。
85
85
  ProductController.php
86
86
  ```ここに言語を入力
87
- // ログイン後の画面遷移先を設定
87
+ if ($event->getResponse() !== null)
88
- $app->setLoginTargetPath($app->url('product_detail', array('id' => $Product->getId())));
89
- $app['session']->getFlashBag()->set('eccube.add.favorite', true);
88
+ {
90
- return $app->redirect($app->url('mypage_login'));
89
+ return $event->getResponse();
91
- }
90
+ }
92
- } elseif ($addCartData['mode'] === 'add_cart') {
93
91
 
92
+ return $app->redirect($app->url('cart'));
93
+ }
94
- log_info('カート追加処理開始', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity']));
94
+ elseif ($addCartData['mode'] === 'clear-cart')
95
+ {
96
+ try
97
+ {
98
+ $app['eccube.service.cart']->clear()->save();
99
+ }
95
100
 
96
- try {
97
- $app['eccube.service.cart']->addProduct($addCartData['product_class_id'], $addCartData['quantity'])->save();
98
- } catch (CartException $e) {
101
+ catch(CartException $e)
102
+ {
99
- log_info('カート追加エラー', array($e->getMessage()));
103
+ log_info('カートクリアエラー', array(
100
- $app->addRequestError($e->getMessage());
104
+ $e->getMessage()
105
+ ));
101
- }
106
+ }
102
107
 
103
- log_info('カート追加処理完了', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity']));
104
-
105
- $event = new EventArgs(
106
- array(
107
- 'form' => $form,
108
- 'Product' => $Product,
109
- ),
110
- $request
111
- );
112
- $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_COMPLETE, $event);
113
-
114
- if ($event->getResponse() !== null) {
115
- return $event->getResponse();
116
- }
117
-
118
- return $app->redirect($app->url('cart'));
108
+ return $app->redirect($app->url('product_detail', array(
119
- }
120
- elseif ($addCartData['mode'] === 'clear-cart') {
121
- try {
122
- $app['eccube.service.cart']->clear()->save();
109
+ 'id' => $Product->getId()
123
- } catch (CartException $e) {
110
+ )));
124
- log_info('カートクリアエラー', array($e->getMessage()));
125
111
  }
126
- return $app->redirect($app->url('product_detail', array('id' => $Product->getId())));
127
- }
112
+ }
128
- }
113
+ }
129
- } else {
114
+ else
115
+ {
130
- $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite');
116
+ $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite');```
131
- if (!empty($addFavorite)) {
132
- // お気に入り登録時にログインされていない場合、ログイン後にお気に入り追加処理を行う
133
- ```
134
117
  AddCartType.php
135
118
  ```ここに言語を入力
136
- /**
137
- * validate
119
+ public
138
- *
139
- * @param type $data
140
- * @param ExecutionContext $context
141
- */
142
- public function validate($data, ExecutionContext $context)
143
- {
144
- if ($mode !== 'add_favorite' && $mode !== 'clear-cart') {
145
- $context->validateValue($data['product_class_id'], array(
146
- new Assert\NotBlank(),
147
- ), '[product_class_id]');
148
- if ($this->Product->getClassName1()) {
149
- $context->validateValue($data['classcategory_id1'], array(
150
- new Assert\NotBlank(),
151
- new Assert\NotEqualTo(array(
152
- 'value' => '__unselected',
153
- 'message' => 'form.type.select.notselect'
154
- )),
155
- ), '[classcategory_id1]');
156
- }
157
- //商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
158
120
 
121
+ function validate($data, ExecutionContext $context)
122
+ {
123
+ if ($mode !== 'add_favorite' && $mode !== 'clear-cart')
124
+ {
125
+ $context->validateValue($data['product_class_id'], array(
126
+ new AssertNotBlank() ,
127
+ ) , '[product_class_id]');
128
+ if ($this->Product->getClassName1())
129
+ {
130
+ $context->validateValue($data['classcategory_id1'], array(
131
+ new AssertNotBlank() ,
132
+ new AssertNotEqualTo(array(
133
+ 'value' => '__unselected',
134
+ 'message' => 'form.type.select.notselect'
135
+ )) ,
136
+ ) , '[classcategory_id1]');
137
+ }
159
138
  ```

5

PHPの追記

2018/06/09 10:45

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -130,4 +130,30 @@
130
130
  $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite');
131
131
  if (!empty($addFavorite)) {
132
132
  // お気に入り登録時にログインされていない場合、ログイン後にお気に入り追加処理を行う
133
+ ```
134
+ AddCartType.php
135
+ ```ここに言語を入力
136
+ /**
137
+ * validate
138
+ *
139
+ * @param type $data
140
+ * @param ExecutionContext $context
141
+ */
142
+ public function validate($data, ExecutionContext $context)
143
+ {
144
+ if ($mode !== 'add_favorite' && $mode !== 'clear-cart') {
145
+ $context->validateValue($data['product_class_id'], array(
146
+ new Assert\NotBlank(),
147
+ ), '[product_class_id]');
148
+ if ($this->Product->getClassName1()) {
149
+ $context->validateValue($data['classcategory_id1'], array(
150
+ new Assert\NotBlank(),
151
+ new Assert\NotEqualTo(array(
152
+ 'value' => '__unselected',
153
+ 'message' => 'form.type.select.notselect'
154
+ )),
155
+ ), '[classcategory_id1]');
156
+ }
157
+ //商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
158
+
133
159
  ```

4

phpファイルの記述

2018/06/09 09:01

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -81,4 +81,53 @@
81
81
  ・上記で定義したコントローラーでclearする。
82
82
  ・元のページにリダイレクト
83
83
 
84
+ ※6/9追記です。
85
+ ProductController.php
86
+ ```ここに言語を入力
87
+ // ログイン後の画面遷移先を設定
88
+ $app->setLoginTargetPath($app->url('product_detail', array('id' => $Product->getId())));
84
- src/Eccube/Service/CartService.phpのコードは文字数オーバーになってしまうため記載できませんでした。。
89
+ $app['session']->getFlashBag()->set('eccube.add.favorite', true);
90
+ return $app->redirect($app->url('mypage_login'));
91
+ }
92
+ } elseif ($addCartData['mode'] === 'add_cart') {
93
+
94
+ log_info('カート追加処理開始', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity']));
95
+
96
+ try {
97
+ $app['eccube.service.cart']->addProduct($addCartData['product_class_id'], $addCartData['quantity'])->save();
98
+ } catch (CartException $e) {
99
+ log_info('カート追加エラー', array($e->getMessage()));
100
+ $app->addRequestError($e->getMessage());
101
+ }
102
+
103
+ log_info('カート追加処理完了', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity']));
104
+
105
+ $event = new EventArgs(
106
+ array(
107
+ 'form' => $form,
108
+ 'Product' => $Product,
109
+ ),
110
+ $request
111
+ );
112
+ $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_COMPLETE, $event);
113
+
114
+ if ($event->getResponse() !== null) {
115
+ return $event->getResponse();
116
+ }
117
+
118
+ return $app->redirect($app->url('cart'));
119
+ }
120
+ elseif ($addCartData['mode'] === 'clear-cart') {
121
+ try {
122
+ $app['eccube.service.cart']->clear()->save();
123
+ } catch (CartException $e) {
124
+ log_info('カートクリアエラー', array($e->getMessage()));
125
+ }
126
+ return $app->redirect($app->url('product_detail', array('id' => $Product->getId())));
127
+ }
128
+ }
129
+ } else {
130
+ $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite');
131
+ if (!empty($addFavorite)) {
132
+ // お気に入り登録時にログインされていない場合、ログイン後にお気に入り追加処理を行う
133
+ ```

3

タグの追加を致しました。

2018/06/09 08:58

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
File without changes

2

要点を簡潔にまとめました。

2018/06/09 03:19

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -55,4 +55,30 @@
55
55
 
56
56
  カートクリアプラグインは商品詳細ページに設置できますが、
57
57
  削除処理後のリダイレクト先が、商品詳細ページでないので、
58
- なんとか商品詳細ページにリダイレクトさせたいというのが実現したいことです。
58
+ なんとか商品詳細ページにリダイレクトさせたいというのが実現したいことです。
59
+
60
+ ※6/8追記2です。
61
+ 自分の環境を前提でお話すると分かりにくいようなので、デモを利用してご説明しますと、
62
+ 例えばhttps://demo3.ec-cube.net/products/detail/23で商品をカートに入れた場合、
63
+ ショッピングカート(https://demo3.ec-cube.net/cart)に移動します。
64
+ この時、画面遷移せずに買い物カゴの商品が増え、
65
+ なおかつ買い物カゴの商品を空にできる削除ボタンが欲しいということです。
66
+ 画面遷移させないプラグインはあるので、あとは買い物カゴの商品を削除する仕組みがあればと。。。
67
+ 現状はショッピングカート(https://demo3.ec-cube.net/cart)の画面でないと商品の削除ができないため、
68
+ 商品詳細ページ(https://demo3.ec-cube.net/products/detail/23)で買い物カゴの中身を削除する機能が
69
+ あれば使い勝手が良くなるのではと思った次第です。
70
+
71
+ EC-CUBEコミュニティで教えていただいたのは以下の部分です。
72
+
73
+ > 引用テキスト
74
+ カートの中身を消すには基本的に、src/Eccube/Service/CartService.phpというクラスのclearメソッドを呼べば良いので、3系ではさほど難しくはございません。
75
+ 下記のように使用できます。
76
+ $app['eccube.service.cart']->clear()->save();
77
+ ボタンを押したときにこのメソッドが呼ばれるようにカスタマイズしてやればOKです。
78
+ > 引用テキスト
79
+ ・削除ボタンをカートブロック内に追加
80
+ ・削除ボタンを押すと、カート商品削除専用URLへ遷移(URLは・FrontControllerProviderでコントローラーを定義。/Block/CartClearController.phpで問題ございません)
81
+ ・上記で定義したコントローラーでclearする。
82
+ ・元のページにリダイレクト
83
+
84
+ src/Eccube/Service/CartService.phpのコードは文字数オーバーになってしまうため記載できませんでした。。

1

皆様からご指摘頂いた件について

2018/06/08 13:01

投稿

MyQuestioner
MyQuestioner

スコア57

title CHANGED
File without changes
body CHANGED
@@ -37,4 +37,22 @@
37
37
  ```
38
38
  これを何とか取り入れて商品詳細ページ(product_detail)にリダイレクトさせたいのですが、相当難しいのでしょうか?
39
39
  カートの商品を削除した後に商品詳細ページ(product_detail)に戻ってくる方法を教えて頂きたいです。
40
- よろしくお願いいたします。
40
+ よろしくお願いいたします。
41
+
42
+ ※6/8追記です。
43
+ マルチポストの件は知りませんでした。
44
+ 大変申し訳ありません。
45
+ EC-CUBEコミュニティで投稿した内容は以下のページです。
46
+ https://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=20466&forum=2
47
+
48
+ プラグインは改造しないほうが良いとのご指摘をいただきましたが、
49
+ カート削除後のリダイレクト処理を変更するという1点だけなので可能なのかなと考えていました。
50
+
51
+ 自分がどうして商品詳細ページにリダイレクトさせたいかといいますと、カート遷移しませんプラグインというプラグインを使用して
52
+ 購入手続き画面に行く前に合計金額と商品の数量を表示させてから
53
+ 「購入へ進む」ボタンを押すようにしてあるからです。
54
+ この時に商品の数を間違えたという場合に、いったんカートをクリアできる削除ボタンがあれば便利なのではないかと。。。
55
+
56
+ カートクリアプラグインは商品詳細ページに設置できますが、
57
+ 削除処理後のリダイレクト先が、商品詳細ページでないので、
58
+ なんとか商品詳細ページにリダイレクトさせたいというのが実現したいことです。