Search code examples
shopware

Update customField in all languages simultaneously


Is there a way to simultaneously update a customField and all of its translations?
Right now the code looks like this (from a cli command)

$context = Context::createDefaultContext();
$data = [[
    'id' => $productId,
    'customFields' => [
        'product_is_new' => true
    ]
]];
$this->productRepository->update($data, $context);

This only updates the custom field in the main language, I suppose this is because of the default context. What is the way to go to update the customField and all of its translations?


Solution

  • You need to provide CustomFields for each language. E.G.

    $data = [
            'id' => $productId,
            'translations' => [
                'languageId1' => [
                    'customFields' => [
                        'product_is_new' => true
                    ]
                ],
                'languageId2' => [
                    'customFields' => [
                        'product_is_new' => true
                    ]
                ],
            ]
    ];
    $this->productRepository->update($data, $context);