I try to create Products via a Admin Module, which works good so far but I can't pass translations for other languages. I followed this guide: https://shopware.stoplight.io/docs/admin-api/docs/guides/writing-entities/associations.md#translated-fields
That's my code:
createProduct() {
const getTaxId = () => {
const criteria = new Criteria();
this.taxRepository.create('tax');
return this.taxRepository.search(criteria, Shopware.Context.api);
};
getTaxId().then(result => {
const taxId = result[0].id;
const newProduct = this.productRepository.create(Shopware.Context.api);
const randomProductHash = Math.floor(Math.random() * 100000);
newProduct.name = 'Das TestProdukt ' + randomProductHash;
newProduct.taxId = taxId;
// translation setted like in the guide
newProduct.translations = {
'2fbb5fe2e29a4d70aa5854ce7ce3e20b': {
name: 'Das TestProdukt ' + randomProductHash,
description: 'english description by code',
},
'2d7d7d698f634a04b5796e49aa846ae6': {
name: 'The testProdckt ' + randomProductHash,
description: 'german description by code',
},
};
newProduct.price = [
{
currencyId: 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
extensions: [],
gross: (Math.random() * 500 + 500).toFixed(2),
linked: true,
listPrice: null,
net: (Math.random() * 250 + 250).toFixed(2),
},
{
currencyId: 'f772f2a317324871a9bb441c34ca0094',
extensions: [],
gross: (Math.random() * 500 + 500).toFixed(2),
linked: true,
listPrice: null,
net: (Math.random() * 250 + 250).toFixed(2),
},
];
newProduct.productNumber = 'DEMO_' + Math.floor(Math.random() * 100000);
newProduct.stock = 1;
console.log(newProduct);
const context = Shopware.Context.api;
context.systemLanguageId = '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
this.productRepository.save(newProduct, context).then(e => {
console.log(e);
});
});
But when I submit it I get the error:
changeset-generator.data.js?13e4:177 - Uncaught (in promise) TypeError: draft.forEach is not a function
at ChangesetGenerator.handleOneToMany (changeset-generator.data.js?13e4:177)
at eval (changeset-generator.data.js?13e4:98)
at eval (entity-definition.data.js?1f64:119)
at Array.forEach (<anonymous>)
at EntityDefinition.forEachField (entity-definition.data.js?1f64:118)
at ChangesetGenerator.recursion (changeset-generator.data.js?13e4:53)
at ChangesetGenerator.generate (changeset-generator.data.js?13e4:34)
at Repository.save (repository.data.js?1d07:103)
at eval (index.js?cce3:158)
It looks like the forEach try to loop through the "translations" object, which is of course not possible as a object. So how have the structure of the translations object have to be?
OK I figured it out, I don't know if the documentation is just wrong or I looked in the wrong space. But thats how it works:
Instead of settings the newProduct.translation variable you have to add a new Translation to it.
let translation = this.translationRepository.create(Shopware.Context.api);
translation.languageId = '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
translation.name = 'THE';
newProduct.translations.add(translation);
translation = this.translationRepository.create(Shopware.Context.api);
translation.languageId = '2d7d7d698f634a04b5796e49aa846ae6';
translation.name = 'DAS';
newProduct.translations.add(translation);
Additionaly you have to add this to the computed method:
translationRepository() {
return this.repositoryFactory.create('product_translation');
},