Quick dive into Sylius’ products

Antonin Clauzier
Darkmira FR
Published in
4 min readJul 6, 2021

I just started trying out Sylius and it took me some time to figure out how Products and their relations have been implemented.

This blogpost aims to make it easy for newcomers to understand how they work and how each piece is related.

First, let’s break it all down and get a simplified overview with an UML diagram :

If you’ve opened the Product entity in your project files already, you may have been surprised that there was only one thing defined: the function createTranslation. It is because the entity extends Core\Model\Product, that itself extends Product\Model\Product from Sylius. Both implement the ProductInterface, and Core\Model\Product implements the ReviewableProductInterface, as it can be seen here :

The entity itself defines the table name and lives in your project files so you can customize it easily. It also defines the createTranslation method because it returns a ProductTranslation entity that also lives in your project files and also defines its table name.

Let’s break down the lowest level class : Product\Model\Product. It uses TimestampableTrait, ToggleableTrait and TranslatableTrait, I will skip those as they’re pretty straightforward. I will also skip the properties id and code and will focus on the four following, as I’ve struggled understanding what they are and how they work.

  • attributes : it’s an array of AttributeValueInterface. The name is pretty self-explanatory : they are values of attributes.
    Attributes are extra datas (usually extra information) linked to products and they can be multiple data types. AttributeValues are values for these attributes.
    It’s an implementation of the Entity Attribute Value (EAV) data model.
    Let’s see a real example from sylius’ fixtures datas : a product uses the attribute “t_shirt_brand”, it’s a text data type and it’s value is “You are breathtaking” : the brand of the t-shirt is “You are breathtaking”.
    This is really simple, it’s a way to add data to products without modifying the product schema; data that are used for certain products but not of all them.
  • variants : it’s an array of ProductVariantInterface. Variants are common in e-commerce : they are different versions of a product. For example, t-shirts have different sizes : each size will be a ProductVariant.
  • options : it’s an array of ProductOptionInterface. The name is also self-explanatory : they are options for products.
  • associations : it’s an array of ProductAssociationInterface. It’s used to link (associate) products between them. The default product association in Sylius’ fixtures is “similar_products”, to link products that have similarities. It could also be used to group together products that have a special promotion, or a new line of products that just have been released : whatever the reason, the idea is to group products.

Now that we understand better what everything is, there is still one thing that I struggled with for quite some time : the attributes property is holding AttributeVALUES, not just Attributes. If you want, for whatever reason, to retrieve the Attribute itself, you need to use the method getAttribute on an AttributeValue.

Time to dive into Core\Model\Product, it has a few more properties that we will look into.

  • productTaxons : it’s an array of ProductTaxonInterface. ProductTaxons are like categories, but as a tree. As always, looking into Sylius’ fixtures we can see that there is a ProductTaxon t_shirts, that have 2 children mens_t_shirts and womens_t_shirt. Taxons are a way to categorize products so users can search for specific types of products easily.
  • channels : it’s an array of ChannelInterface. Channels are “places” where you’re selling your products. The default one is the web store, but you could have a channel for resellers buying from you where you would use a different tax calculation strategy and where you would have different prices.
  • mainTaxon : it’s a BaseTaxonInterface. I’ve already explained ProductTaxons, this property just stores the main one.

I won’t explain reviews, averageRating and images as they are self-explanatory and I don’t have anything special to say for them.

Now you should understand how Products work better, what every property is and how it affects products. Even when I worked 3 and a half years in e-commerce I had to wrap my head around Sylius’ product and I hope that this article helped you dive into it faster and understand it better at first glance.

--

--