API Customization: How to embed the ProductVariant resource in the output for /shop/products

How can I change the serialization of the variants field during calling /api/v2/shop/products so that it directly returns the ProductVariant resource inside the same output instead of only the id/IRI?

Answer from Slack message here: https://sylius-devs.slack.com/archives/C5Q1H5SNT/p1635148077007900?thread_ts=1635003943.007500&cid=C5Q1H5SNT

In Sylius you need to to the following:

  • Copy the vendor/sylius/sylius/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ProductVariant.xml file to your custom serialization project folder
  • Add the group: <group>shop:product:read</group> to the properites you need

Do the same for the ProductVariantTranslation if needed.

For more details check also: https://api-platform.com/docs/core/serialization/#embedding-relations