简体   繁体   中英

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!

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.

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;
    }
  );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM