Replies: 1 comment
-
Hi @svrizkyp you can refer to this guide in the documentation on how to extend a service. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello guys,
I'd like to extend the cart service by adding a simple function in the current cart service function like below :
backend\node_modules@medusajs\medusa\dist\services\cart.js
CartService.prototype.update = function (cartId, data) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () { var cartRepo, relations, cart, originalCartCustomer, customer, shippingAddress_1, countryCode, addrRepo, billingAddress, shippingAddress, previousDiscounts, hasFreeShipping, prevContext, updatedCart; var _this = this; var _a, _b, _c, _d, _e; return __generator(this, function (_f) { switch (_f.label) { case 0: cartRepo = transactionManager.withRepository(this.cartRepository_); relations = [ "items", "items.variant", "items.variant.product", "shipping_methods", "shipping_address", "billing_address", "gift_cards", "customer", "region", "payment_sessions", "region.countries", "discounts", "discounts.rule", ]; if (this.featureFlagRouter_.isFeatureEnabled(sales_channels_1.default.key) && data.sales_channel_id) { relations.push("items.variant", "items.variant.product"); } return [4 /*yield*/, this.retrieve(cartId, { relations: relations, })]; case 1: cart = _f.sent(); **if (data.customer_name) {cart.customer_name = data.customer_name;} if (data.customer_company_name) {cart.customer_company_name = data.customer_company_name;} if (data.ecommerce_type_id) {cart.ecommerce_type_id = data.ecommerce_type_id;} if (data.custom_feature) {cart.custom_feature = data.custom_feature;} if (data.themes_id) {cart.themes_id = data.themes_id;}** originalCartCustomer = __assign({}, ((_a = cart.customer) !== null && _a !== void 0 ? _a : {})); if (!data.customer_id) return [3 /*break*/, 3]; return [4 /*yield*/, this.updateCustomerId_(cart, data.customer_id, data.customer_name)];
I add below code in Case1 function :
if (data.customer_name) {cart.customer_name = data.customer_name;}
if (data.customer_company_name) {cart.customer_company_name = data.customer_company_name;}
if (data.ecommerce_type_id) {cart.ecommerce_type_id = data.ecommerce_type_id;}
if (data.custom_feature) {cart.custom_feature = data.custom_feature;}
if (data.themes_id) {cart.themes_id = data.themes_id;}
It works perfectly after i build the backend, but i want to extend this function in my src/services folder , is there any way to achive this ?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions