shopware6shopware6-api

How can I translate product custom fields using the sync API?


I am creating products with the sync REST API endpoint. I have a few custom fields that require translation. How can I translate them when creating/updating the products?

My basic request to /api/_action/sync looks like this:

[
    {
        "entity": "product",
        "action": "upsert",
        "payload": [
            {
                "productNumber": "12345",
                "name": "A simple product",
                "taxId": "01982e42a48a73bfbf87fd4f7410cadb",
                "stock": 9999,
                "isCloseout": false,
                "weight": 0,
                "visibilities": [],
                "price": [],
                "translations": {
                    "cs-CZ": {
                        "description": ""
                    },
                    "de-DE": {
                        "description": ""
                    },
                    "en-GB": {
                        "description": ""
                    },
                    "es-ES": {
                        "description": ""
                    },
                    "nl-NL": {
                        "description": ""
                    },
                    "pl-PL": {
                        "description": ""
                    }
                },
                "properties": [],
                "id": "bbc2966bdeb5085837f24824b93eca42",
                "tags": [],
                "customFields": {
                    "zenit_gravity_tabs_set_short_description": "Foo"
                }
            }
        ]
    }
]

I would like to translate zenit_gravity_tabs_set_short_description. I have already tried adding it to the translations like this:

{
                    "cs-CZ": {
                        "description": "",
                        "zenit_gravity_tabs_set_short_description": "Foo"
                    },
                    "de-DE": {
                        "description": ""
                    },
                    "en-GB": {
                        "description": ""
                    },
                    "es-ES": {
                        "description": ""
                    },
                    "nl-NL": {
                        "description": ""
                    },
                    "pl-PL": {
                        "description": ""
                    }
                }

But that does not add any translations. Also, changing the customFields to:

{
    "zenit_gravity_tabs_set_short_description": {
        "cs-CZ": "Foo"
    }
}

will not work because the value needs to be a string:

{
    "errors": [
        {
            "code": "ba785a8c-82cb-4283-967c-3cf342181b40",
            "status": "400",
            "detail": "Dieser Wert sollte vom Typ string sein.",
            "template": "This value should be of type {{ type }}.",
            "meta": {
                "parameters": {
                    "{{ value }}": "array",
                    "{{ type }}": "string"
                }
            },
            "source": {
                "pointer": "\/0\/0\/translations\/2fbb5fe2e29a4d70aa5854ce7ce3e20b\/customFields\/zenit_gravity_tabs_set_short_description"
            }
        }
    ]
}

Solution

  • Using the translations was already the right idea. Looking at the product_translation table it contains a column for the product fields and as such a column custom_fields which contains a json object with all custom fields and their value. So the correct usage within the translations key is:

    {
        "cs-CZ": {
            "description": "",
            "customFields": {
                "zenit_gravity_tabs_set_short_description": "BAR"
            }
        },
        "de-DE": {
            "description": "",
            "customFields": {
                "zenit_gravity_tabs_set_short_description": "FOO"
            }
        },
        "en-GB": {
            "description": ""
        },
        "es-ES": {
            "description": ""
        },
        "nl-NL": {
            "description": ""
        },
        "pl-PL": {
            "description": ""
        }
    }
    

    customFields is then the usual object of the field as key and the translated string as value.