Reputation: 63
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:
many thanks!
Upvotes: 0
Views: 565
Reputation: 63
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;
}
);
Upvotes: 0
Reputation: 66
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.
Upvotes: 1