Is there an easy way to define property units in Shopware 6?

Viewed 358

so im trying to display product properties with units, that I want to define in the admin panel. I found 'scale units' in settings, but asfaik thats only for products. Adding an additional field also does not seem to be the right way, since you can not define additional fields for properties.

A workaround would be adding the unit in the label, but thats not a good solution imo.

I was not able to find a simple solution to that problem...

2 Answers

May be add a customField to the property options, with reference to the existing unit entity. But in any case you have to write some code to display the unit in the storefront.

edit

Code example to display custom fields on property options:

Component.override('sw-property-option-detail', {
  template,

  data() {
    return {
      ...
      customFieldSets: []
    };
  },

  computed: {
    customFieldSetRepository() {
      return this.repositoryFactory.create('custom_field_set');
    },

    customFieldSetCriteria() {
      const criteria = new Criteria(1, 100);

      criteria.addFilter(Criteria.equals('relations.entityName', 'propertyOption'));
      criteria
        .getAssociation('customFields')
        .addSorting(Criteria.sort('config.customFieldPosition', 'ASC', true));

      return criteria;
    },
  },

  methods: {
    loadEntityData() {
      ...
      this.customFieldSetRepository
      .search(this.customFieldSetCriteria, Shopware.Context.api)
      .then((result) => {
        this.customFieldSets = result.filter((set) => set.customFields.length > 0);
      });
    },
  }
});
<sw-card :title="$tc('sw-settings-custom-field.general.mainMenuItemGeneral')" v-if="customFieldSets.length > 0" :isLoading="isLoading">
  <sw-custom-field-set-renderer v-if="currentOption" :entity="currentOption" :sets="customFieldSets" :disabled="!allowEdit">
  </sw-custom-field-set-renderer>
</sw-card>

It's a quite specific problem. You probably can use the existing entity for the units and create an association to the properties in code. The easiest solution still will be adding the unit in the label, as you already suggested.

Related