简体   繁体   中英

How disable redirection when add item to cart with SyliusCartBundle in Symfony2?

I'm already implement the basic of SyliusCartBundle ( http://docs.sylius.org/en/latest/bundles/SyliusCartBundle/installation.html )

but the problem is the method to add item to a cart, because the only way that I know is with

<a href="{{ path('sylius_cart_item_add', {'productId': product.id}) }}">Add product to cart</a>

acording to http://docs.sylius.org/en/latest/bundles/SyliusCartBundle/actions.html

and I want to add a item without redirect the webpage.

sorry if is too basic but I only start with symfony 4 day ago.

Just like @zizoujal say is with ajax. But when I ask I was a beginner in MVC and I dont undestand the method.

The solution is:

  1. Create an action that get de information of the cart and only save it, return an json respoonse like {result: success}.
  2. in the view call with ajax this action and if return result== success do the action you want.

Finally I dont use Symfony2 (SyliusCartBundle) but with a little more experience in MCV I can undestand and I implment my own action (in cakephp).

Not the best way to do it but it may help you:

  • Write your own controller, use yourbundle_cart_item_add and add the respective routings.
  • Imitate what it is done in the Sylius/Bundle/CartBundle/Controller/CartItemController:addAction .
  • At the end, instead of redirectToCarSummary() redirect to what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM