お世話になります。
ECサイト構築に興味を持ち、先日EC-CUBE3.0.16をインストールして使用してみました。一つ気になったのが商品をカートに入れると購入画面に進まない限り商品を削除できない点です。
ECサイトを使う側からすれば少し不便かなと思いまして、カートから商品を削除する方法を色々調べてみたのですが、カートクリアボタン追加プラグイン(https://www.ec-cube.net/products/detail.php?product_id=1346)というプラグインを見つけ導入してみました。
しかし、このプラグインはどのページからでも新品を削除できるものではなく、
購入手続きの画面に入ってから機能するプラグインのようです。
EC-CUBEコミュニティで質問をしてみたのですが、返ってきた答えを見ますと自分の能力では無理そうなので、カートクリアボタン追加プラグインをカスタマイズして何とかしようと思ったのですが、リダイレクト方法がわからずに困っています。
カートクリアプラグインにはCartClearController.phpというファイルがあり、その中に「return $app->redirect($app->url('cart'));」という部分があるのですが、cartの部分を商品詳細ページ(product_detail)に変更してリダイレクトさせようするとシステムエラーが起きます。
トップページ(homepage)や商品リスト(product_list)はエラーが出ずにリダイレクトされるのですが、商品詳細ページの場合ページごとにIDが振ってあるため、単にproduct_detailを指定するだけでは上手くいかないようです。
どなたかカートクリアをすると商品詳細ページにリダイレクトされる方法をご存知の方はおられないでしょうか?
カート遷移しませんプラグインというプラグインがあって
以下のように商品をカートに入れた後、ページ遷移せずに商品詳細ページに戻ってくる記述があります。
/** * かーとに商品追加後、商品ページを表示する。 * @param \Eccube\Event\EventArgs $EventArgs */ public function toProductDetail(\Eccube\Event\EventArgs $EventArgs){ if (!$this->app['session']->getFlashBag()->has('eccube.front.request.error')) { $this->app['session']->getFlashBag()->set('eccube.front.cart.success', '購入画面に進む'); } $Product = $EventArgs->getArgument('Product'); $EventArgs->setResponse( new RedirectResponse( $this->app->url('product_detail', array('id' => $Product->getId())) ) ); }
これを何とか取り入れて商品詳細ページ(product_detail)にリダイレクトさせたいのですが、相当難しいのでしょうか?
カートの商品を削除した後に商品詳細ページ(product_detail)に戻ってくる方法を教えて頂きたいです。
よろしくお願いいたします。
※6/8追記です。
マルチポストの件は知りませんでした。
大変申し訳ありません。
EC-CUBEコミュニティで投稿した内容は以下のページです。
https://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=20466&forum=2
プラグインは改造しないほうが良いとのご指摘をいただきましたが、
カート削除後のリダイレクト処理を変更するという1点だけなので可能なのかなと考えていました。
自分がどうして商品詳細ページにリダイレクトさせたいかといいますと、カート遷移しませんプラグインというプラグインを使用して
購入手続き画面に行く前に合計金額と商品の数量を表示させてから
「購入へ進む」ボタンを押すようにしてあるからです。
この時に商品の数を間違えたという場合に、いったんカートをクリアできる削除ボタンがあれば便利なのではないかと。。。
カートクリアプラグインは商品詳細ページに設置できますが、
削除処理後のリダイレクト先が、商品詳細ページでないので、
なんとか商品詳細ページにリダイレクトさせたいというのが実現したいことです。
※6/8追記2です。
自分の環境を前提でお話すると分かりにくいようなので、デモを利用してご説明しますと、
例えばhttps://demo3.ec-cube.net/products/detail/23で商品をカートに入れた場合、
ショッピングカート(https://demo3.ec-cube.net/cart)に移動します。
この時、画面遷移せずに買い物カゴの商品が増え、
なおかつ買い物カゴの商品を空にできる削除ボタンが欲しいということです。
画面遷移させないプラグインはあるので、あとは買い物カゴの商品を削除する仕組みがあればと。。。
現状はショッピングカート(https://demo3.ec-cube.net/cart)の画面でないと商品の削除ができないため、
商品詳細ページ(https://demo3.ec-cube.net/products/detail/23)で買い物カゴの中身を削除する機能が
あれば使い勝手が良くなるのではと思った次第です。
EC-CUBEコミュニティで教えていただいたのは以下の部分です。
引用テキスト
カートの中身を消すには基本的に、src/Eccube/Service/CartService.phpというクラスのclearメソッドを呼べば良いので、3系ではさほど難しくはございません。
下記のように使用できます。
$app['eccube.service.cart']->clear()->save();
ボタンを押したときにこのメソッドが呼ばれるようにカスタマイズしてやればOKです。
引用テキスト
・削除ボタンをカートブロック内に追加
・削除ボタンを押すと、カート商品削除専用URLへ遷移(URLは・FrontControllerProviderでコントローラーを定義。/Block/CartClearController.phpで問題ございません)
・上記で定義したコントローラーでclearする。
・元のページにリダイレクト
※6/9追記です。
ProductController.php
public function detail(Application $app, Request $request, $id) { $BaseInfo = $app['eccube.repository.base_info']->get(); if ($BaseInfo->getNostockHidden() === Constant::ENABLED) { $app['orm.em']->getFilters()->enable('nostock_hidden'); } /* @var $Product \Eccube\Entity\Product */ $Product = $app['eccube.repository.product']->get($id); if (!$request->getSession()->has('_security_admin') && $Product->getStatus()->getId() !== 1) { throw new NotFoundHttpException(); } if (count($Product->getProductClasses()) < 1) { throw new NotFoundHttpException(); } /* @var $builder \Symfony\Component\Form\FormBuilderInterface */ $builder = $app['form.factory']->createNamedBuilder('', 'add_cart', null, array( 'product' => $Product, 'id_add_product_id' => false, )); $event = new EventArgs( array( 'builder' => $builder, 'Product' => $Product, ), $request ); $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE, $event); /* @var $form \Symfony\Component\Form\FormInterface */ $form = $builder->getForm(); if ($request->getMethod() === 'POST') { $form->handleRequest($request); if ($form->isValid()) { $addCartData = $form->getData(); if ($addCartData['mode'] === 'add_favorite') { if ($app->isGranted('ROLE_USER')) { $Customer = $app->user(); $app['eccube.repository.customer_favorite_product']->addFavorite($Customer, $Product); $app['session']->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId()); $event = new EventArgs( array( 'form' => $form, 'Product' => $Product, ), $request ); $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_FAVORITE, $event); if ($event->getResponse() !== null) { return $event->getResponse(); } return $app->redirect($app->url('product_detail', array('id' => $Product->getId()))); } else { // 非会員の場合、ログイン画面を表示 // ログイン後の画面遷移先を設定 $app->setLoginTargetPath($app->url('product_detail', array('id' => $Product->getId()))); $app['session']->getFlashBag()->set('eccube.add.favorite', true); return $app->redirect($app->url('mypage_login')); } } elseif ($addCartData['mode'] === 'add_cart') { log_info('カート追加処理開始', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity'])); try { $app['eccube.service.cart']->addProduct($addCartData['product_class_id'], $addCartData['quantity'])->save(); } catch (CartException $e) { log_info('カート追加エラー', array($e->getMessage())); $app->addRequestError($e->getMessage()); } log_info('カート追加処理完了', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity'])); $event = new EventArgs( array( 'form' => $form, 'Product' => $Product, ), $request ); $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_COMPLETE, $event); if ($event->getResponse() !== null) { return $event->getResponse(); } return $app->redirect($app->url('cart')); } elseif ($addCartData['mode'] === 'clear-cart') { try { $app['eccube.service.cart']->clear()->save(); } catch (CartException $e) { log_info('カートクリアエラー', array($e->getMessage())); } return $app->redirect($app->url('product_detail', array('id' => $Product->getId()))); } } } else { $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite'); if (!empty($addFavorite)) { // お気に入り登録時にログインされていない場合、ログイン後にお気に入り追加処理を行う if ($app->isGranted('ROLE_USER')) { $Customer = $app->user(); $app['eccube.repository.customer_favorite_product']->addFavorite($Customer, $Product); $app['session']->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId()); } } }
AddCartType.php
public function validate($data, ExecutionContext $context) { if ($mode !== 'add_favorite' && $mode !== 'clear-cart') { $context->validateValue($data['product_class_id'], array( new Assert\NotBlank(), ), '[product_class_id]'); if ($this->Product->getClassName1()) { $context->validateValue($data['classcategory_id1'], array( new Assert\NotBlank(), new Assert\NotEqualTo(array( 'value' => '__unselected', 'message' => 'form.type.select.notselect' )), ), '[classcategory_id1]'); } //商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない if ($this->Product->getClassName2()) { $context->validateValue($data['classcategory_id2'], array( new Assert\NotBlank(), new Assert\NotEqualTo(array( 'value' => '__unselected', 'message' => 'form.type.select.notselect' )), ), '[classcategory_id2]'); } } } }
回答1件
あなたの回答
tips
プレビュー