In this episode, we dive into setting up product variations for our e-commerce platform—which is probably the trickiest part of the whole project! Managing things like sizes and colors for products can get really complex, but we're tackling it with a simple approach using an adjacency list for our variations.
We start by updating our example product to something more real, like a pair of Nike Air Force 1s, and then move on to building the variation model and corresponding migration. You’ll see how we structure our variations in the database with fields like title, type (color, size, etc.), price, SKU, parent_id (for nesting), and order (for display purposes). We walk through populating the database with some color and size variations, demonstrating how to nest sizes (like 8 and 9) under their respective colors (black and white), and making sure each final selection has a SKU.
From there, we hook up the relationships in our models so products know about their variations (and vice versa), and we dump out all these variations on the product page. At first, it’s just a flat list, but then we pull in a recursive trait (the same one we used for categories) to group and order the variations properly—mimicking the kind of drop downs and grouping you see on real online stores.
By the end of this video, we’ve got the data structure and relationships set up so that users can select, for instance, a black shoe and then the available sizes for that color. Next up, we'll move this logic out of our template and start working with Livewire to make it interactive!