I have created a custom field called OrderType
, now my task is to submit the selected order type to the database while creating an order in shopware administration panel.
How can I do it?
I have tried with setting the selectedCustomField to the Context but it did not saved.
I have no idea how can I pull order object and add my custom field before saving it to database.
My new component:
Component.override('sw-order-create-options', {
template,
inject: [
'repositoryFactory',
'acl',
'customFieldDataProviderService',
'feature',
],
props: {
context: {
type: Object,
required: true,
},
},
data() {
return {
customFieldSets: null,
selectedCustomField: null,
};
},
computed: {
orderRepository() {
return this.repositoryFactory.create('order');
},
customFieldSetRepository() {
return this.repositoryFactory.create('custom_field');
},
customFieldSetCriteria() {
const criteria = new Criteria();
console.log(this.context)
criteria.addFilter(Criteria.equals('name', 'order_custom_field_order_type'));
return criteria;
},
customFieldOptions() {
if (!this.customFieldSets) {
return [];
}
const fieldSet = this.customFieldSets[0]; // Assuming only one set is fetched
return fieldSet.config.options.map(option => ({
value: option.value,
label: option.label
}));
}
},
watch: {
context: {
handler(newVal) {
console.log('Context changed:', newVal);
},
deep: true,
},
selectedCustomField(newVal) {
console.log('Selected Custom Field:', newVal);
}
},
created() {
this.createdComponent();
},
methods: {
createdComponent() {
this.loadCustomFieldSets();
},
loadCustomFieldSets() {
this.customFieldSetRepository.search(this.customFieldSetCriteria, Shopware.Context.api)
.then((customFieldSets) => {
this.customFieldSets = customFieldSets;
});
},
},
});
and twig js file:
{% block sw_order_create_options_order %}
<sw-single-select
v-if="customFieldSets"
:options="customFieldOptions"
v-model:value="selectedCustomField"
label="Select Custom Field"
placeholder="Please select an orderType"
/>
{% endblock %}