Search code examples
vue.jssymfonyshopwareshopware6

Custom Product Extension Data Not Saving in Administration


I'm developing a plugin for Shopware 6 that extends the product specifications tab with additional fields. The data is loaded correctly when data is manually inserted into the database tables (entity_extension and entity_extension_translation). However, when there is no existing data, the administration interface does not allow me to input and save new data. The data appears to be initialized correctly, but it is not being saved to the database when I click the default save button of the product editor in the sw6 administration.

Steps I’ve Taken:

  • Extending the product detail specifications tab.
  • Initializing a new entity if no existing data is found.
  • Setting the initialized data in the product.extensions.catalogueBoxData property.

Here is my Component.override:

const {Component} = Shopware;
const {Criteria} = Shopware.Data;
const Context = Shopware.Context;

import template from "./sw-product-extension-detail-specifications.html.twig";

Component.override('sw-product-detail-specifications', {
    template,

    inject: ['repositoryFactory'],

    data() {
        return {
            productId: null,
            startingAidData: null
        };
    },

    created() {
        this.productId = this.$route.params.id;
        this.loadExtension();
    },

    computed: {
        wucPluginStartingAidExtensionCriteria() {
            const criteria = new Criteria();
            criteria.addFilter(Criteria.equals('productId', this.productId));
            return criteria;
        },

        wucPluginStartingAidExtensionRepository() {
            return this.repositoryFactory.create('wuc_plugin_starting_aid_extension');
        }
    },

    methods: {
        loadExtension() {
            this.wucPluginStartingAidExtensionRepository
                .search(this.wucPluginStartingAidExtensionCriteria, Context.api)
                .then(startingAidData => {
                    if (startingAidData.last()) {
                        console.log('Data found', startingAidData.last());
                        this.product.extensions.startingAidData = startingAidData.last();
                    } else {
                        console.log('Create and Set New Entity');
                        this.startingAidData = this.wucPluginStartingAidExtensionRepository.create(Shopware.Context.api);
                        this.startingAidData.productId = this.productId;
                        this.startingAidData.headline = 'Test!'
                        this.product.extensions.startingAidData = this.startingAidData;
                    }
                })
                .catch(error => {
                    console.error('Error loading extension:', error);
                });
        }
    }
});

This is the according template:

{% block sw_product_detail_specifications_measures_packaging %}
        <sw-card  v-if="product.extensions.startingAidData" title="Starting Aid Test"
                  position-identifier="sw-product-detail-specifications-starting-aid"
        >
            <sw-text-field
                v-model:value="product.extensions.startingAidData.headline"
                class="sw-product-starting-aid__headline-field"
                label="Starting Aid Test"
                placeholder="Enter headline"
            ></sw-text-field>
        </sw-card>
    {% parent %}
{% endblock %}

How can I ensure that the startingAid extension is correctly saved to the database when i save the product in the administration? What might be missing or incorrectly implemented in my current approach?

Thanks in advance, Niklas

Problem Description:

  • The new entity is initialized in the frontend when no existing data is found.
  • The new entity appears in product.extensions.catalogueBoxData.
  • When saving the product, the new entity is not persisted in the database.

Solution

  • Solution:

    The root cause of the issue was an incorrect assignment of the extension data. To resolve this, it was essential to use the precise name of the custom product extension entity when updating the product data.

    Instead of

    this.product.extensions.startingAidData = this.startingAidData; 
    

    The correct approach was:

    this.product.extensions.wucPluginStartingAidExtension = this.startingAidData;
    

    By referencing the extension using its defined entity name (wucPluginStartingAidExtension), the system can accurately associate and persist the custom data within the product's extension structure.