docs: document conditional shipping option prices + expand on price rules (#12217)
* docs: document conditional shipping option prices + expand on price rules * fix errors
This commit is contained in:
@@ -628,7 +628,7 @@ export const deductStepHighlights = [
|
||||
```ts title="src/workflows/steps/deduct-purchase-points.ts" highlights={deductStepHighlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
createStep,
|
||||
StepResponse
|
||||
StepResponse,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { LOYALTY_MODULE } from "../../modules/loyalty"
|
||||
import LoyaltyModuleService from "../../modules/loyalty/service"
|
||||
@@ -641,7 +641,7 @@ type DeductPurchasePointsInput = {
|
||||
export const deductPurchasePointsStep = createStep(
|
||||
"deduct-purchase-points",
|
||||
async ({
|
||||
customer_id, amount
|
||||
customer_id, amount,
|
||||
}: DeductPurchasePointsInput, { container }) => {
|
||||
const loyaltyModuleService: LoyaltyModuleService = container.resolve(
|
||||
LOYALTY_MODULE
|
||||
@@ -658,7 +658,7 @@ export const deductPurchasePointsStep = createStep(
|
||||
|
||||
return new StepResponse(result, {
|
||||
customer_id,
|
||||
points: pointsToDeduct
|
||||
points: pointsToDeduct,
|
||||
})
|
||||
},
|
||||
async (data, { container }) => {
|
||||
@@ -748,7 +748,7 @@ export const addPurchaseAsPointsStep = createStep(
|
||||
|
||||
return new StepResponse(result, {
|
||||
customer_id: input.customer_id,
|
||||
points: pointsToAdd
|
||||
points: pointsToAdd,
|
||||
})
|
||||
},
|
||||
async (data, { container }) => {
|
||||
@@ -868,18 +868,18 @@ export const handleOrderPointsWorkflow = createWorkflow(
|
||||
"cart.promotions.*",
|
||||
"cart.promotions.rules.*",
|
||||
"cart.promotions.rules.values.*",
|
||||
"cart.promotions.application_method.*"
|
||||
"cart.promotions.application_method.*",
|
||||
],
|
||||
filters: {
|
||||
id: order_id
|
||||
id: order_id,
|
||||
},
|
||||
options: {
|
||||
throwIfKeyNotFound: true
|
||||
}
|
||||
throwIfKeyNotFound: true,
|
||||
},
|
||||
})
|
||||
|
||||
validateCustomerExistsStep({
|
||||
customer: orders[0].customer
|
||||
customer: orders[0].customer,
|
||||
} as ValidateCustomerExistsStepInput)
|
||||
|
||||
const loyaltyPointsPromotion = getCartLoyaltyPromoStep({
|
||||
@@ -893,14 +893,14 @@ export const handleOrderPointsWorkflow = createWorkflow(
|
||||
.then(() => {
|
||||
deductPurchasePointsStep({
|
||||
customer_id: orders[0].customer!.id,
|
||||
amount: loyaltyPointsPromotion.application_method!.value as number
|
||||
amount: loyaltyPointsPromotion.application_method!.value as number,
|
||||
})
|
||||
|
||||
updatePromotionsStep([
|
||||
{
|
||||
id: loyaltyPointsPromotion.id,
|
||||
status: "inactive",
|
||||
}
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
@@ -912,7 +912,7 @@ export const handleOrderPointsWorkflow = createWorkflow(
|
||||
.then(() => {
|
||||
addPurchaseAsPointsStep({
|
||||
customer_id: orders[0].customer!.id,
|
||||
amount: orders[0].total
|
||||
amount: orders[0].total,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -1056,10 +1056,10 @@ So, to create an API route at the path `/store/customers/me/loyalty-points`, cre
|
||||
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse
|
||||
} from "@medusajs/framework/http";
|
||||
import { LOYALTY_MODULE } from "../../../../../modules/loyalty";
|
||||
import LoyaltyModuleService from "../../../../../modules/loyalty/service";
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
import { LOYALTY_MODULE } from "../../../../../modules/loyalty"
|
||||
import LoyaltyModuleService from "../../../../../modules/loyalty/service"
|
||||
|
||||
export async function GET(
|
||||
req: AuthenticatedMedusaRequest,
|
||||
@@ -1405,17 +1405,17 @@ import {
|
||||
createPromotionsStep,
|
||||
updateCartPromotionsWorkflow,
|
||||
updateCartsStep,
|
||||
useQueryGraphStep
|
||||
useQueryGraphStep,
|
||||
} from "@medusajs/medusa/core-flows"
|
||||
import {
|
||||
validateCustomerExistsStep,
|
||||
ValidateCustomerExistsStepInput
|
||||
ValidateCustomerExistsStepInput,
|
||||
} from "./steps/validate-customer-exists"
|
||||
import {
|
||||
getCartLoyaltyPromoAmountStep,
|
||||
GetCartLoyaltyPromoAmountStepInput
|
||||
GetCartLoyaltyPromoAmountStepInput,
|
||||
} from "./steps/get-cart-loyalty-promo-amount"
|
||||
import { CartData, CUSTOMER_ID_PROMOTION_RULE_ATTRIBUTE, } from "../utils/promo"
|
||||
import { CartData, CUSTOMER_ID_PROMOTION_RULE_ATTRIBUTE } from "../utils/promo"
|
||||
import { CreatePromotionDTO } from "@medusajs/framework/types"
|
||||
import { PromotionActions } from "@medusajs/framework/utils"
|
||||
import { getCartLoyaltyPromoStep } from "./steps/get-cart-loyalty-promo"
|
||||
@@ -1433,7 +1433,7 @@ const fields = [
|
||||
"promotions.rules.values.*",
|
||||
"currency_code",
|
||||
"total",
|
||||
"metadata"
|
||||
"metadata",
|
||||
]
|
||||
|
||||
export const applyLoyaltyOnCartWorkflow = createWorkflow(
|
||||
@@ -1444,24 +1444,24 @@ export const applyLoyaltyOnCartWorkflow = createWorkflow(
|
||||
entity: "cart",
|
||||
fields,
|
||||
filters: {
|
||||
id: input.cart_id
|
||||
id: input.cart_id,
|
||||
},
|
||||
options: {
|
||||
throwIfKeyNotFound: true
|
||||
}
|
||||
throwIfKeyNotFound: true,
|
||||
},
|
||||
})
|
||||
|
||||
validateCustomerExistsStep({
|
||||
customer: carts[0].customer
|
||||
customer: carts[0].customer,
|
||||
} as ValidateCustomerExistsStepInput)
|
||||
|
||||
getCartLoyaltyPromoStep({
|
||||
cart: carts[0] as unknown as CartData,
|
||||
throwErrorOn: "found"
|
||||
throwErrorOn: "found",
|
||||
})
|
||||
|
||||
const amount = getCartLoyaltyPromoAmountStep({
|
||||
cart: carts[0]
|
||||
cart: carts[0],
|
||||
} as unknown as GetCartLoyaltyPromoAmountStepInput)
|
||||
|
||||
// TODO create and apply the promotion on the cart
|
||||
@@ -1491,7 +1491,7 @@ export const prepareLoyaltyPromoDataHighlights = [
|
||||
```ts title="src/workflows/apply-loyalty-on-cart.ts" highlights={prepareLoyaltyPromoDataHighlights}
|
||||
const promoToCreate = transform({
|
||||
carts,
|
||||
amount
|
||||
amount,
|
||||
}, (data) => {
|
||||
const randomStr = Math.random().toString(36).substring(2, 8)
|
||||
const uniqueId = (
|
||||
@@ -1512,8 +1512,8 @@ const promoToCreate = transform({
|
||||
{
|
||||
attribute: CUSTOMER_ID_PROMOTION_RULE_ATTRIBUTE,
|
||||
operator: "eq",
|
||||
values: [data.carts[0].customer!.id]
|
||||
}
|
||||
values: [data.carts[0].customer!.id],
|
||||
},
|
||||
],
|
||||
campaign: {
|
||||
name: uniqueId,
|
||||
@@ -1521,9 +1521,9 @@ const promoToCreate = transform({
|
||||
campaign_identifier: uniqueId,
|
||||
budget: {
|
||||
type: "usage",
|
||||
limit: 1
|
||||
}
|
||||
}
|
||||
limit: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1559,17 +1559,17 @@ export const createLoyaltyPromoStepHighlights = [
|
||||
|
||||
```ts title="src/workflows/apply-loyalty-on-cart.ts" highlights={createLoyaltyPromoStepHighlights}
|
||||
const loyaltyPromo = createPromotionsStep([
|
||||
promoToCreate
|
||||
promoToCreate,
|
||||
] as CreatePromotionDTO[])
|
||||
|
||||
const { metadata, ...updatePromoData } = transform({
|
||||
carts,
|
||||
promoToCreate,
|
||||
loyaltyPromo
|
||||
loyaltyPromo,
|
||||
}, (data) => {
|
||||
const promos = [
|
||||
...(data.carts[0].promotions?.map((promo) => promo?.code).filter(Boolean) || []) as string[],
|
||||
data.promoToCreate.code
|
||||
data.promoToCreate.code,
|
||||
]
|
||||
|
||||
return {
|
||||
@@ -1577,20 +1577,20 @@ const { metadata, ...updatePromoData } = transform({
|
||||
promo_codes: promos,
|
||||
action: PromotionActions.ADD,
|
||||
metadata: {
|
||||
loyalty_promo_id: data.loyaltyPromo[0].id
|
||||
}
|
||||
loyalty_promo_id: data.loyaltyPromo[0].id,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
updateCartPromotionsWorkflow.runAsStep({
|
||||
input: updatePromoData
|
||||
input: updatePromoData,
|
||||
})
|
||||
|
||||
updateCartsStep([
|
||||
{
|
||||
id: input.cart_id,
|
||||
metadata
|
||||
}
|
||||
metadata,
|
||||
},
|
||||
])
|
||||
|
||||
// retrieve cart with updated promotions
|
||||
@@ -1621,8 +1621,8 @@ Next, you'll create the API route that executes this workflow.
|
||||
To create the API route, create the file `src/api/store/carts/[id]/loyalty-points/route.ts` with the following content:
|
||||
|
||||
```ts title="src/api/store/carts/[id]/loyalty-points/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http";
|
||||
import { applyLoyaltyOnCartWorkflow } from "../../../../../workflows/apply-loyalty-on-cart";
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { applyLoyaltyOnCartWorkflow } from "../../../../../workflows/apply-loyalty-on-cart"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
@@ -1633,8 +1633,8 @@ export async function POST(
|
||||
const { result: cart } = await applyLoyaltyOnCartWorkflow(req.scope)
|
||||
.run({
|
||||
input: {
|
||||
cart_id
|
||||
}
|
||||
cart_id,
|
||||
},
|
||||
})
|
||||
|
||||
res.json({ cart })
|
||||
@@ -1808,13 +1808,13 @@ export const removeLoyaltyFromCartWorkflowHighlights = [
|
||||
import {
|
||||
createWorkflow,
|
||||
transform,
|
||||
WorkflowResponse
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import {
|
||||
useQueryGraphStep,
|
||||
updateCartPromotionsWorkflow,
|
||||
updateCartsStep,
|
||||
updatePromotionsStep
|
||||
updatePromotionsStep,
|
||||
} from "@medusajs/medusa/core-flows"
|
||||
import { getCartLoyaltyPromoStep } from "./steps/get-cart-loyalty-promo"
|
||||
import { PromotionActions } from "@medusajs/framework/utils"
|
||||
@@ -1833,7 +1833,7 @@ const fields = [
|
||||
"promotions.rules.values.*",
|
||||
"currency_code",
|
||||
"total",
|
||||
"metadata"
|
||||
"metadata",
|
||||
]
|
||||
|
||||
export const removeLoyaltyFromCartWorkflow = createWorkflow(
|
||||
@@ -1844,46 +1844,46 @@ export const removeLoyaltyFromCartWorkflow = createWorkflow(
|
||||
entity: "cart",
|
||||
fields,
|
||||
filters: {
|
||||
id: input.cart_id
|
||||
}
|
||||
id: input.cart_id,
|
||||
},
|
||||
})
|
||||
|
||||
const loyaltyPromo = getCartLoyaltyPromoStep({
|
||||
cart: carts[0] as unknown as CartData,
|
||||
throwErrorOn: "not-found"
|
||||
throwErrorOn: "not-found",
|
||||
})
|
||||
|
||||
updateCartPromotionsWorkflow.runAsStep({
|
||||
input: {
|
||||
cart_id: input.cart_id,
|
||||
promo_codes: [loyaltyPromo.code!],
|
||||
action: PromotionActions.REMOVE
|
||||
}
|
||||
action: PromotionActions.REMOVE,
|
||||
},
|
||||
})
|
||||
|
||||
const newMetadata = transform({
|
||||
carts
|
||||
carts,
|
||||
}, (data) => {
|
||||
const { loyalty_promo_id, ...rest } = data.carts[0].metadata || {}
|
||||
|
||||
return {
|
||||
...rest,
|
||||
loyalty_promo_id: null
|
||||
loyalty_promo_id: null,
|
||||
}
|
||||
})
|
||||
|
||||
updateCartsStep([
|
||||
{
|
||||
id: input.cart_id,
|
||||
metadata: newMetadata
|
||||
}
|
||||
metadata: newMetadata,
|
||||
},
|
||||
])
|
||||
|
||||
updatePromotionsStep([
|
||||
{
|
||||
id: loyaltyPromo.id,
|
||||
status: "inactive"
|
||||
}
|
||||
status: "inactive",
|
||||
},
|
||||
])
|
||||
|
||||
// retrieve cart with updated promotions
|
||||
@@ -1920,7 +1920,7 @@ To create the API route, add the following in `src/api/store/carts/[id]/loyalty-
|
||||
|
||||
```ts title="src/api/store/carts/[id]/loyalty-points/route.ts"
|
||||
// other imports...
|
||||
import { removeLoyaltyFromCartWorkflow } from "../../../../../workflows/remove-loyalty-from-cart";
|
||||
import { removeLoyaltyFromCartWorkflow } from "../../../../../workflows/remove-loyalty-from-cart"
|
||||
|
||||
// ...
|
||||
export async function DELETE(
|
||||
@@ -1932,8 +1932,8 @@ export async function DELETE(
|
||||
const { result: cart } = await removeLoyaltyFromCartWorkflow(req.scope)
|
||||
.run({
|
||||
input: {
|
||||
cart_id
|
||||
}
|
||||
cart_id,
|
||||
},
|
||||
})
|
||||
|
||||
res.json({ cart })
|
||||
@@ -2033,11 +2033,11 @@ export const completeCartWorkflowHookHighlights = [
|
||||
]
|
||||
|
||||
```ts title="src/workflows/hooks/complete-cart.ts" highlights={completeCartWorkflowHookHighlights} collapsibleLines="1-6" expandButtonLabel="Show Imports"
|
||||
import { completeCartWorkflow } from "@medusajs/medusa/core-flows";
|
||||
import LoyaltyModuleService from "../../modules/loyalty/service";
|
||||
import { LOYALTY_MODULE } from "../../modules/loyalty";
|
||||
import { CartData, getCartLoyaltyPromotion } from "../../utils/promo";
|
||||
import { MedusaError } from "@medusajs/framework/utils";
|
||||
import { completeCartWorkflow } from "@medusajs/medusa/core-flows"
|
||||
import LoyaltyModuleService from "../../modules/loyalty/service"
|
||||
import { LOYALTY_MODULE } from "../../modules/loyalty"
|
||||
import { CartData, getCartLoyaltyPromotion } from "../../utils/promo"
|
||||
import { MedusaError } from "@medusajs/framework/utils"
|
||||
|
||||
completeCartWorkflow.hooks.validate(
|
||||
async ({ cart }, { container }) => {
|
||||
@@ -2055,13 +2055,13 @@ completeCartWorkflow.hooks.validate(
|
||||
"promotions.rules.*",
|
||||
"promotions.rules.values.*",
|
||||
"promotions.application_method.*",
|
||||
"metadata"
|
||||
"metadata",
|
||||
],
|
||||
filters: {
|
||||
id: cart.id
|
||||
}
|
||||
id: cart.id,
|
||||
},
|
||||
}, {
|
||||
throwIfKeyNotFound: true
|
||||
throwIfKeyNotFound: true,
|
||||
})
|
||||
|
||||
const loyaltyPromo = getCartLoyaltyPromotion(
|
||||
|
||||
Reference in New Issue
Block a user