DEV Community

loading...
Cover image for Introduction | Building a Shopping Cart with Symfony

Introduction | Building a Shopping Cart with Symfony

qferrer profile image Quentin ・2 min read

Welcome to my first tutorial to learn the popular PHP Symfony Framework by creating a simple Shopping Cart from scratch.

A shopping cart on a website facilitates the purchase of a product or service.

Alt Text

Why this tutorial?

I've been developing with Symfony for more than 8 years and I think it's time for me to share my experience with you through this series of articles. I made this tutorial for you to learn Symfony by doing. I have always learned to use a framework or a library by creating personal or professional projects.

Also, writing this tutorial was an exercise for me. Indeed, knowing how to use Symfony is one thing, knowing how to explain how to use Symfony is another.

What is it about?

The tutorial describes the creation of a shopping cart, from scratch and step by step. We will manage the basic shopping cart features:

  • Add products to the cart,
  • Update the quantity of products in the cart,
  • Remove products from the cart,
  • Clear the cart,
  • See the list of products in the cart,
  • See the quantity of products in the cart,
  • See the summary of the cart.

The checkout and order process won't be developed but let me know if you are interested, I will try to do it and add some additional steps to this tutorial. Also, the design is not the goal of this project, we will just make it functional.

We will try to respect the best practices and use the base concepts of Symfony to allow you to reuse code and create your projects independently.

What you will learn?

  • Create a project with Symfony 5 (from scratch),
  • Build a Shopping Cart using Symfony 5,
  • Use the most popular Symfony components,
  • Create static and dynamic forms with Symfony Form,
  • Create pages with Twig,
  • Store data in session using the Session service,
  • Create and register Services in Symfony,
  • Create and Persist entities in a database using Doctrine,
  • Write custom DQL queries using the Query Builder of Doctrine,
  • Validate entities with Symfony Validator,
  • Load fake data with Doctrine Fixtures,
  • Create a CLI command with Symfony Console,
  • Write functional tests with Symfony Browser Kit and PHPUnit.

Who this tutorial is for?

  • For PHP developers who want to discover Symfony 5 using a concrete and complex project,
  • Developers who want to go beyond the documentation offered by Symfony and take action.

Where are the tutorial resources?

I created a repository that contains all the code of the shopping cart. If you want, you can clone it somewhere on your local machine:

$ git clone https://github.com/qferr/happy-shop
Enter fullscreen mode Exit fullscreen mode

Ready to code? Let's start to install and configure our working environment in the first step.

Discussion (5)

pic
Editor guide
Collapse
earlfaneuf profile image
Earl Faneuf

I have resolved all the small issues with this sample to make it work for me. That you so much for giving me this starting point. It has been a great help for me and will make things work quite well. My comments earlier regarding not being able to connect back to the cart. For others trying to do the same, I simply went to my userchecker. I check if the cart has a userid in it (I added that earlier) and if it does, I then get the cartid which has that userid. Then add it back into the session. Works like a charm.

Collapse
earlfaneuf profile image
Earl Faneuf

I had a hard time unsuccessfully attempting to figure out how to add a link on a product page to just ajax to the backend and add that product to the cart yet stay on the product page. Not the ajax part, that was easy. I mean how to pull apart the code to add to cart in the background (in that one case, bypassing the detail page.

Second, more important. I have added the code to send the total to paypal. Works like a charm. But now I am thinking how to I see what this userid has in the recorded shopping cart. There seems to be no indicator in the Order and OrderItem controllers to save the user, so as an admin, I can look at the products bought after the fact. I see that the code saves the order info into the database, but there is nothing in there to save who did the order LOL

Would be nice to have a tiny bit of assistance :-)

Collapse
earlfaneuf profile image
Earl Faneuf

The issue, with a bit of thinking, came to me and that now works like a charm too. The author is wonderful to supply this basic shopping cart.

Latest issue was trying to figure out how to reconnect the user back to the cart when they logout and back in. The data is still in the tables (thank God) but I am not sure how to reconnect it back to the logged in user. I am thinking you must be using a session variable set and I need to figure out how you are doing this so I can then reconnect the user on login.

That might be my last issue with the cart. I am extending it with my extra needs easily now that I figured out how to add the userid to the existing cart. I needed that so I can work on the payment side with paypal. Need to mark the items sold. So need to know who bought them. Of course the WHO is also for a real world shopping cart, rather critical since I need to know where to ship what they bought!! LOL

Collapse
furopi profile image
furopi

Congratulations, dear Sir! Thats probably the best tutorial I read in my life, ever!

Just a random guy from Germany passing by.... Kind regards!

Collapse
qferrer profile image
Quentin Author

Glad you enjoyed it! Thanks for your feedback.