Change the Product properties

305 Views Asked by At

I'm using (http://localhost/store-api/product/{productId}) this endpoint to fetch the specific product of shopware 6 by passing productId. I have to change the product variant when I click on the properties of the product.

Screenshot of the product with properties

I tried to get the product id with different way but can't. How can I change the product?

1

There are 1 best solutions below

0
On

By default this should already happen. Is there a reason specific you chose not to use the existing functionality of variants and switching thereof and to rebuild it instead?

Anyway, you can retrieve the id of a variant by the set of corresponding option properties. To do so there's a route of the storefront you can use.

GET https://example.com/detail/{parentId}/switch?options={options}

parentId is the id of the parent of the currently viewed product: product.parentId

options is a url encoded json object with key value pairs of property group id to property option id of all property options assigned to the variant, that you want to get the id of.

{
   "{groupId}": "{optionId}"
}

Example for your case:

{
   "b1c9a2dddbac43198f32183edc197e61": "739a59b89fd341c6a6d49d349dae56e6", // size
   "90c834ef82d1467ca1c36965672e5b6c": "00000c51e2c44960b4eb07ede8c1f486" // color
}

Let's say b1c9a2dddbac43198f32183edc197e61 would be the id of the property group "Size", then 739a59b89fd341c6a6d49d349dae56e6 would be the id of the selected size option.

If done correctly the response will give you the SEO url and id to the variant associated with the options:

{
    "url":"https:\/\/example.com\/Practical-Bronze-Socks-Appeal\/9fc58b6c2a074e4ba0a46b9b471dff1b",
    "productId":"9fc58b6c2a074e4ba0a46b9b471dff1b"
}