Sunday, October 16, 2022

[FIXED] How Do I Update The Shopware 6 Sales Channel Table When It Refuses To Update Via The Admin API

Issue

I have been trying to update the 'payment_method_ids' column of the 'sales_channel' table of Shopware 6 via the Vue JS administration API without it working or even generating any error. But, when I update the 'payment_method_id' column of the same 'sales_channel' table, it works just fine.

Below is my code:

const { Component} = Shopware;
import template from './store-settings-page.html.twig';
Component.register('store-settings-page', {
   template,
   inject: [
    'repositoryFactory'
    ],

   metaInfo() {
      return {
          title: this.$createTitle()
      };
    },

  data() {
    return {
        salesChannelId: '73049sdus783993hdjnncsfm',
        paymentMethodId: 'f6dd50230aa145adb6b73801d4bcf31b',
        paymentMethodIds: ["6f4bbcbcdcfc4e449ee4c4904dc797e9", "979cad06654c475bb7bbb997f7c81774", "ba893622d71b4d25adde336862367297", "f6dd50230aa145adb6b73801d4bcf31b"],
    }
   },
   computed: {
    salesChannelRepository() {
        return this.repositoryFactory.create('sales_channel');
    },
   },
   methods: {
    updatePaymentMethodId(){
        this.salesChannelRepository
            .get(this.salesChannelId, Shopware.Context.api)
            .then((update) => {
                update.paymentMethodId = this.paymentMethodId;
                this.salesChannelRepository.save(update, Shopware.Context.api);
            });  
    },
    updatePaymentMethodIds(){
        this.salesChannelRepository
            .get(this.salesChannelId, Shopware.Context.api)
            .then((update) => {
                update.paymentMethodIds = this.paymentMethodIds;
                this.salesChannelRepository.save(update, Shopware.Context.api);
            });    
    },
   },
   created() {
    this.updatePaymentMethodIds();
   }
})

Solution

paymentMethodIds is a ManyToManyIdField of the SalesChannelDefinition. These fields represent a special case. The content for these fields is computed server-side by the corresponding EntityIndexer instance. So when you assign a payment method to the sales channel, the column of the paymentMethodIds should automatically be updated.

If you want to add payment methods programmatically, you should use the actual association field instead:

// create a new payment method entity
const paymentMethod = this.repositoryFactory.create('payment_method')
    .create(Shopware.Context.api, this.paymentMethodId);
// or fetch an existing one
// const paymentMethod = await this.repositoryFactory.create('payment_method')
//     .get(this.paymentMethodId, Shopware.Context.api);

const criteria = new Criteria(1, 1);
criteria.addAssociation('paymentMethods');

this.salesChannelRepository
    .get(this.salesChannelId, Shopware.Context.api, criteria)
    .then((update) => {
        update.paymentMethods.add(paymentMethod);
        this.salesChannelRepository.save(update, Shopware.Context.api);
    });


Answered By - dneustadt
Answer Checked By - Candace Johnson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.