In this episode, we're diving into the process of actually creating an order in our checkout flow. We start by testing the checkout both as a guest and a signed-in user, making it easier on ourselves by pre-filling some of the required data instead of typing it every time. Along the way, we spot and fix a couple of minor issues—like a mistake with model relationships and making sure our shipping address is correctly attached.
Once that's sorted, we focus on building the order itself, starting with creating an order object and gradually attaching all necessary information, like the user's email (whether they're logged in or not), the subtotal from the shopping cart, and associating the order with a user if authenticated. We add the required relationships to the Order model for the user, shipping type, and shipping address so that everything is properly linked together.
After ensuring all the associations work, we take care of a quick bug where some timestamps weren't set or nullable as they should be, and we fix this both at the migration and model level. Then, it's just testing things out by creating orders as both a guest and an authenticated user, and verifying that all the right info is stored in the database: shipping type, shipping address, subtotal, and (when available) the user.
By the end of the episode, your app is now successfully creating complete orders with all the right data associated—ready for us to attach the actual basket contents in the next episode.