i'm completely new to shopware, not very familiar with javascript and struggling with the sw-entity-multi-select field.
I want to save the selected Items to my extension, but i havn't find an easy solution for that, instead i'm doing some awful Object.assign stuff, so i can save at least something.
Question 1: How can i save properly selections to my extension?
Question 2: How can i properly display the saved selections
Here is my html.twig:
{% block sw_product_detail_attribute_sets %}
{% parent() %}
<div v-if="!isLoading && parentProduct && product">
<sw-card :title="$tc('sw-product.detail.requiredProductsCardLabel')"
:isLoading="isLoading">
<sw-inherit-wrapper
v-if="!isLoading"
v-model="RPExtension"
:has-parent="!!parentProduct.id"
:inherited-value="parentRPExtension"
>
<template #content="{ isInherited }">
<sw-entity-multi-select
v-if="!isLoading"
entity="product"
:key="isInherited"
:entity-collection="products"
@change="setProducts"
:placeholder="$tc('sw-product.detail.requiredProductsPlaceholder')">
</sw-entity-multi-select>
</sw-inherit-wrapper>
</sw-card>
</div>
{% endblock %}
And also the index.js for the html.twig:
import template from './sw-product-detail-requiredProducts.html.twig';
const { Component, Context } = Shopware;
const { mapState, mapGetters } = Component.getComponentHelper();
const { EntityCollection, Criteria } = Shopware.Data;
Component.override('sw-product-detail-base', {
template,
inject: ['repositoryFactory'],
data() {
return {
products: null,
};
},
computed: {
...mapState('swProductDetail', [
'product',
'parentProduct',
]),
...mapGetters('swProductDetail', [
'isLoading',
]),
productRepository() {
console.log('productRepository');
return this.repositoryFactory.create('product');
},
requiredProductsRepository() {
return this.repositoryFactory.create('product_preconditions')
},
productContext() {
return { ...Shopware.Context.api, inheritance: true };
},
RPExtension: {
get: function() {
if (this.product && this.product.extensions && this.product.extensions['requiredProducts']) {
return (
this.product.extensions['requiredProducts']['productPrecondition']
);
}
return null;
},
set: function(value) {
this.$set(this.product.extensions['requiredProducts'], 'productPrecondition', value ?? null);
}
},
parentRPExtension() {
if (this.parentProduct && this.parentProduct.extensions && this.parentProduct.extensions['requiredProducts']) {
return (
this.product.extensions['requiredProducts']['productPrecondition']
);
}
return null;
},
},
created() {
this.createdComponent();
},
methods: {
createdComponent() {
this.products = new EntityCollection(
this.productRepository.route,
this.productRepository.entityName,
Shopware.Context.api,
);
},
setProducts(selectedProducts) {
this.products = selectedProducts;
this.products.forEach((selectedProduct) => {
const precondition = this.createProductPreconditionEntity(selectedProduct);
this.product.extensions.requiredProducts.push(precondition);
});
},
createProductPreconditionEntity(selectedProduct) {
const productPreconditions = this.requiredProductsRepository.create(Context.api);
Object.assign(productPreconditions, {
productId: this.product.id,
productPrecondition: selectedProduct.id,
directPrecondition: false
});
return productPreconditions;
},
},
});