Search code examples
shopwareshopware6

Insert customFields using the Administration


I have created a plugin in the adminstration and I want set value of customFields for a specific product. How can I do that ?


Solution

  • Just use the product repository to fetch the product, set the custom fields value and persist it using the repository again.

    Simplified example:

    Component.register('my-component', {
        template,
    
        inject: ['repositoryFactory'],
    
        computed: {
            productRepository() {
                return this.repositoryFactory.create('product');
            },
        },
    
        methods: {
            async updateProduct(productId, value) {
                const product = await this.productRepository.get(productId, Shopware.Context.api);
                
                if (!product) {
                    return;
                }
                
                product.customFields.my_custom_field = value;
    
                this.productRepository.save(product, Shopware.Context.api);
            }
        },
    });