关于现有服务的扩展可能性,我有一个相当技术性的问题:
特别是,我需要更改cart-store-api.api.service.js服务中一个方法的功能。API调用还应该传输item.payload字段,以便在管理程序中创建lineItems时启用传递自定义有效负载:
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
},
],
};
}我找到了本指南,解释了如何装饰服务,但是我没有成功地扩展cartStoreApi服务,因为它似乎没有“技术名称”(我找到了这些globals.types.ts,其中为aclService分配了一个类似密钥的'acl‘)。所以我的问题是我不能装饰cartStoreService,因为我不知道它的引用标识符。
这就产生了两个问题:
非常感谢!
发布于 2022-07-18 06:40:07
扩展和装饰服务是有很大区别的。如果它满足了您的需要,您可以简单地通过扩展它来使用服务,就像CartStoreService扩展ApiService一样。然后,您可以使用它作为您的自定义服务在您的插件。如果您真的想要修饰它,这意味着您将修改从任何地方对服务的每个调用,可能只是尝试访问服务容器中的服务容器,就像文档中所指示的那样,我不确定globals.types.ts是否完成。
发布于 2022-07-18 11:47:15
好吧,我解决了我的问题。这就是解决方案的样子:
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;
}
);https://stackoverflow.com/questions/73014308
复制相似问题