DEV Community

Discussion on: Removing Products from the Cart | Building a Shopping Cart with Symfony

Collapse
 
isabellebengrine profile image
Isabelle Bengrine

Are you sure this works? I am only a beginner but if I am not mistaken, this does not modify the cart saved in the database, but shouldn't it? The remove button for the cart items is not working at all. And the clear button for the total cart removes the order items but as it does not remove it from the session or from the database, if I click on the cart again, I will find the cart as it was... I do appreciate your efforts in putting together this tutorial bit I would be very grateful if you could let me know if I am missing something here or if I am right? Thanks in advance!

Collapse
 
qferrer profile image
Quentin Ferrer

Hi Isabelle ! Yes I'm sure that it works. If you want to test, you can install the repository that contains all the code about this tutorial: github.com/qferr/happy-shop. Could you drop your code to take a look? Thanks!

Collapse
 
isabellebengrine profile image
Isabelle Bengrine

Hi Quentin! Thanks a lot for your quick reply! I am looking at the repository and working on my code to make it work! I hope I will find what is wrong with it! Thanks again for all the time you spent working on this tutorial (and sorry I asked you if this worked !)

Collapse
 
isabellebengrine profile image
Isabelle Bengrine

Hi Quentin! I am sorry to bother you again but actually, I am wondering how your database gets updated when I see no 'persist' or 'flush' anywhere? In my code, the cart page gets wiped out when I hit remove (for one item) or clear (for all items at once), but the cart is actually still the same in the database and I think this is why I get the cart back when I hit the cart button; but in your project, the cart is really updated and that is what I also want to do; so how come the database is modified just by clicking on the buttons? did I miss a step that may be so basic for you that you did not mention it perhaps (as I said, I am a beginner so it is totally possible)? Again, thanks in advance!

Thread Thread
 
qferrer profile image
Quentin Ferrer

Hello Isabelle. Let me explain how does cart clearing work:

  1. When the user click on the clear button, the form is submitted and the CartController::index() method is invoked thanks to the Symfony routing.
  2. We get the current cart, using the CartManager manager:
    • If the cart is already associated with the user in the session, we use it,
    • If the cart does not exist in the session, a new cart is created using the CartFactory factory and becomes the current cart. Note that it will be associated with the user in the session after being persisted in the database (see CartManager::save() method).
  3. The current cart is binded to the form using the FormFactory factory and then the submitted data are merged into the Order object thanks to the Form::handleRequest() method. Next, the form triggers a post submit event to let you manipulate the form data, the current cart in this case.
  4. As we have registered a ClearCartListener listener to the Cart Form, the ClearCartListener::postSubmit() method is invoked. So, we clear the cart using the form data (the current cart by reference) if the clear button has been clicked.
  5. At this time, the cart is not yet saved in the database so let's go back to the controller. If the form is submitted and valid, we save the cart modifications using the CartManager::save() method.
  6. Finally, we redirect the user to the cart page.

Let me know if you need more information.

Have a good day.

Thread Thread
 
isabellebengrine profile image
Isabelle Bengrine • Edited

Thanks for this reply! of course, I realized later that the persist and flush parts are in the save method of the cartmanager! this was silly of me to ask about that... now if you are curious, I finally found what was preventing the remove and clear buttons to work properly: (oddly this did not prevent your project from working just fine, whereas mine could not): in the twig file we had : form_end(form, {'render_rest': false})
and I found on stackoverflow some comments about a similar issue to mine that said:
"But to submit it separately you must ensure that you're building the "complete" child form in your view" and looking at the forms, I thought we are displaying all the fields anyway, so why do we need this? so I removed the ", {'render_rest': false}" part and now it works!!!
I still do not quite understand why I had some trouble and you did not but this just shows I have so much to learn!!! anyway, again thanks for your help and your patience!!! I love your tutorials!!!