Products
The Product model in Sylius represents unique items in your store. Every product can have different variants (e.g., size, color) and attributes (e.g., material, brand). Each product must have at least one variant to be sold in the store.
How to create a Product?
Before we learn how to create products that can be sold, let’s see how to create a product without its complex dependencies.
Creating an empty product is not enough to save it in the database. It needs to have a name
, a code
and a slug
.
Although the product is now added to the system, it cannot yet be purchased by customers because it lacks variants.
Variants
A ProductVariant represents a unique version of a product (e.g., a T-shirt in size Medium). Variants can have their own pricing configurations, inventory tracking, and more.
You can create variants based on product options (e.g., size, color).
You can also create variants without using options, giving you flexibility in how you manage product versions.
Virtual Product Variants, that do not require shipping
If a product does not require shipping (e.g., a digital download or software), you can set the shippingRequired
property to false
on its ProductVariant.
How to create a Product with a Variant?
If you need to sell products in different forms (e.g., hardcover vs. paperback), you can create a product with variants as follows:
Create the base product as shown previously.
Create a variant using the ProductVariantFactory.
Set the necessary attributes for the variant:
Finally, save the variant to the database:
Options
When managing products with different variations (e.g., T-shirts in various sizes and colors), you’ll need to define ProductOptions. Each option can have multiple ProductOptionValues.
Example Options
Size: S, M, L, XL, XXL
Color: Red, Green, Blue
After defining the options, Sylius can automatically generate product variants based on the possible combinations.
How to create a Product with Options and Variants?
Here’s how to set up a product with options (e.g., color) and automatically generate variants for it.
After you have an Option created and you keep it as $option
variable let’s add it to the Product and generate Variants.
Last updated