Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
extending cartStoreService in shopware 6

I have quite a technical question regarding the extension possibilities of existing services:

Particularly, I need to change the functionality of one method in the cart-store-api.api.service.js service. The API call should transmit the item.payload field as well to enable passing custom payload when creating lineItems in the admin:

getPayloadForItem(item, salesChannelId, isNewProductItem, id) {
    let dummyPrice = null;
    if (this.shouldPriceUpdated(item, isNewProductItem)) {
        dummyPrice = deepCopyObject(item.priceDefinition);
        dummyPrice.taxRules = item.priceDefinition.taxRules;
        dummyPrice.quantity = item.quantity;
        dummyPrice.type = this.mapLineItemTypeToPriceType(item.type);
    }

    return {
        items: [
            {
                id: id,
                referencedId: id,
                label: item.label,
                quantity: item.quantity,
                type: item.type,
                description: item.description,
                priceDefinition: dummyPrice,
                stackable: true,
                removable: true,
                salesChannelId,
                // this is what I want to add
                payload: item.payload
            },
        ],
    };
}

I found this guide explaining how services can be decorated, HOWEVER I did not manage to extend the cartStoreApi service because it does not seem to have a 'technical name' (I found these globals.types.ts where many services are assigned a key - like 'acl' for the aclService). So my problem is that I cannot decorate the cartStoreService because I dont know its reference identifier.

Two questions arise from this:

  1. Are my observations correct? Or is there a way to decorate the cartStoreApi?
  2. If this is indeed not possible, what is the suggested way to solve my issue?

many thanks!

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

There is quite a difference between extending and decorating a service. If it suites your needs, you can use the service simply by extending it, like CartStoreService extends ApiService. Then you can use it as your custom service in your plugin. If you really want to decorate it, which means you will modify every call to the service from anywhere, maybe just try to access it in the service container like indicated in the docs, I'm not sure the globals.types.ts is complete.

about 4 years ago · Santiago Gelvez Report

0

Ok, I managed to solve my issue. This is what the solution looks like:

Shopware.Application.addServiceProviderDecorator(
    "cartStoreService",
    (service) => {
      const decoratedMethod = service.getPayloadForItem;
  
      service.getPayloadForItem = function (item, salesChannelId, isNewProductItem, id) {
        const returnValue = decoratedMethod.call(service, item, salesChannelId, isNewProductItem, id)
        returnValue.items[0]['payload'] = item.payload // <-- reason for this whole decoration: pass custom payload to API 
        
        return returnValue
      };
  
      return service;
    }
  );
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!