docs: fixes to wishlist plugin guide (#11551)

This commit is contained in:
Shahed Nasser authored and GitHub committed 2025-02-20 19:44:51 +02:00
1 parent 3b4997840e
commit cfffd55ae6
2 files changed
+73 -22

No files matched your search

@@ -933,6 +933,12 @@ The workflow to add an item to a wishlist has the following steps:
depth: 1, depth: 1,
link: "/references/helper-steps/useQueryGraphStep" link: "/references/helper-steps/useQueryGraphStep"
}, },
{
type: "step",
name: "validateWishlistExistsStep",
description: "Validate that the customer's wishlist exists.",
depth: 1
},
{ {
type: "step", type: "step",
name: "validateWishlistSalesChannelStep", name: "validateWishlistSalesChannelStep",
@@ -965,9 +971,42 @@ The workflow to add an item to a wishlist has the following steps:
The `useQueryGraphStep` is from Medusa's workflows package. So, you'll only implement the other steps. The `useQueryGraphStep` is from Medusa's workflows package. So, you'll only implement the other steps.
#### validateWishlistExistsStep
The second step in the workflow validates that the customer's wishlist, retrieved in the first step, exists.
To create the step, create the file `src/workflows/steps/validate-wishlist-exists.ts` with the following content:
![Directory structure after adding the step file](https://res.cloudinary.com/dza7lstvk/image/upload/v1740071251/Medusa%20Resources/wishlist-29_bq6kcn.jpg)
```ts title="src/workflows/steps/validate-wishlist-exists.ts"
import { MedusaError } from "@medusajs/framework/utils"
import { createStep } from "@medusajs/framework/workflows-sdk"
import { InferTypeOf } from "@medusajs/framework/types"
import { Wishlist } from "../../modules/wishlist/models/wishlist"
type Input = {
wishlists?: InferTypeOf<typeof Wishlist>[]
}
export const validateWishlistExistsStep = createStep(
"validate-wishlist-exists",
async (input: Input) => {
if (!input.wishlists?.length) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
"No wishlist found for this customer"
)
}
}
)
```
This step receives an array of wishlists and throws an error if it's empty. You'll use this to stop the workflow's execution if the customer doesn't have a wishlist.
#### validateWishlistSalesChannelStep #### validateWishlistSalesChannelStep
The second step in the workflow validates that the wishlist belongs to the sales channel specified in the input. The third step in the workflow validates that the wishlist belongs to the sales channel specified in the input.
To create the step, create the file `src/workflows/steps/validate-wishlist-sales-channel.ts` with the following content: To create the step, create the file `src/workflows/steps/validate-wishlist-sales-channel.ts` with the following content:
@@ -1079,7 +1118,7 @@ This step receives the variant ID, sales channel ID, and wishlist object as inpu
#### createWishlistItemStep #### createWishlistItemStep
The fourth step in the workflow creates a wishlist item for the specified variant in the wishlist. The fifth step in the workflow creates a wishlist item for the specified variant in the wishlist.
Create the file `src/workflows/steps/create-wishlist-item.ts` with the following content: Create the file `src/workflows/steps/create-wishlist-item.ts` with the following content:
@@ -1133,12 +1172,13 @@ You can now add the `createWishlistItemWorkflow` to the plugin. Create the file
![Directory structure after adding the workflow file](https://res.cloudinary.com/dza7lstvk/image/upload/v1737470660/Medusa%20Resources/wishlist-16_ovujwp.jpg) ![Directory structure after adding the workflow file](https://res.cloudinary.com/dza7lstvk/image/upload/v1737470660/Medusa%20Resources/wishlist-16_ovujwp.jpg)
export const createWishlistItemWorkflowHighlights = [ export const createWishlistItemWorkflowHighlights = [
["16", "useQueryGraphStep", "Retrieve the wishlist of the specified customer."], ["17", "useQueryGraphStep", "Retrieve the wishlist of the specified customer."],
["27", "validateWishlistSalesChannelStep", "Validate that the wishlist belongs to the specified sales channel."], ["25", "validateWishlistExistsStep", "Validate that the customer has a wishlist."],
["33", "validateVariantWishlistStep", "Validate that the specified variant is not already in the wishlist."], ["29", "validateWishlistSalesChannelStep", "Validate that the wishlist belongs to the specified sales channel."],
["39", "createWishlistItemStep", "Create the wishlist item."], ["35", "validateVariantWishlistStep", "Validate that the specified variant is not already in the wishlist."],
["45", "useQueryGraphStep", "Retrieve the wishlist again with the new item added."], ["41", "createWishlistItemStep", "Create the wishlist item."],
["54", "wishlist", "Return the wishlist with the new item."] ["47", "useQueryGraphStep", "Retrieve the wishlist again with the new item added."],
["56", "wishlist", "Return the wishlist with the new item."]
] ]
```ts title="src/workflows/create-wishlist-item.ts" collapsibleLines="1-6" expandButtonLabel="Show Imports" highlights={createWishlistItemWorkflowHighlights} ```ts title="src/workflows/create-wishlist-item.ts" collapsibleLines="1-6" expandButtonLabel="Show Imports" highlights={createWishlistItemWorkflowHighlights}
@@ -1147,6 +1187,7 @@ import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
import { validateWishlistSalesChannelStep } from "./steps/validate-wishlist-sales-channel" import { validateWishlistSalesChannelStep } from "./steps/validate-wishlist-sales-channel"
import { createWishlistItemStep } from "./steps/create-wishlist-item" import { createWishlistItemStep } from "./steps/create-wishlist-item"
import { validateVariantWishlistStep } from "./steps/validate-variant-wishlist" import { validateVariantWishlistStep } from "./steps/validate-variant-wishlist"
import { validateWishlistExistsStep } from "./steps/validate-wishlist-exists"
type CreateWishlistItemWorkflowInput = { type CreateWishlistItemWorkflowInput = {
variant_id: string variant_id: string
@@ -1163,9 +1204,10 @@ export const createWishlistItemWorkflow = createWorkflow(
filters: { filters: {
customer_id: input.customer_id, customer_id: input.customer_id,
}, },
options: { })
throwIfKeyNotFound: true,
}, validateWishlistExistsStep({
wishlists
}) })
validateWishlistSalesChannelStep({ validateWishlistSalesChannelStep({
@@ -1406,6 +1448,12 @@ The workflow to remove an item from a wishlist has the following steps:
depth: 1, depth: 1,
link: "/references/helper-steps/useQueryGraphStep" link: "/references/helper-steps/useQueryGraphStep"
}, },
{
type: "step",
name: "validateWishlistExistsStep",
description: "Validate that the customer's wishlist exists.",
depth: 1
},
{ {
type: "step", type: "step",
name: "validateItemInWishlistStep", name: "validateItemInWishlistStep",
@@ -1430,7 +1478,7 @@ The workflow to remove an item from a wishlist has the following steps:
hideLegend hideLegend
/> />
The `useQueryGraphStep` is from Medusa's workflows package. So, you'll only implement the other steps. The `useQueryGraphStep` is from Medusa's workflows package, and you implemented the `validateWishlistExistsStep` [previously](#validatewishlistexistsstep) . So, you'll only implement the other steps.
#### validateItemInWishlistStep #### validateItemInWishlistStep
@@ -1524,11 +1572,12 @@ You can now add the `deleteWishlistItemWorkflow` to the plugin. Create the file
![Directory structure after adding the workflow file](https://res.cloudinary.com/dza7lstvk/image/upload/v1737474872/Medusa%20Resources/wishlist-22_wt1g36.jpg) ![Directory structure after adding the workflow file](https://res.cloudinary.com/dza7lstvk/image/upload/v1737474872/Medusa%20Resources/wishlist-22_wt1g36.jpg)
export const deleteWishlistItemWorkflowHighlights = [ export const deleteWishlistItemWorkflowHighlights = [
["14", "useQueryGraphStep", "Retrieve the wishlist of a customer."], ["15", "useQueryGraphStep", "Retrieve the wishlist of a customer."],
["25", "validateItemInWishlistStep", "Validate that the item is in the customer's wishlist."], ["23", "validateWishlistExistsStep", "Validate that the customer has a wishlist."],
["30", "deleteWishlistItemStep", "Delete the wishlist item."], ["27", "validateItemInWishlistStep", "Validate that the item is in the customer's wishlist."],
["33", "useQueryGraphStep", "Retrieve the wishlist again with the item removed."], ["32", "deleteWishlistItemStep", "Delete the wishlist item."],
["42", "wishlist", "Return the wishlist without the removed item."] ["35", "useQueryGraphStep", "Retrieve the wishlist again with the item removed."],
["44", "wishlist", "Return the wishlist without the removed item."]
] ]
```ts title="src/workflows/delete-wishlist-item.ts" highlights={deleteWishlistItemWorkflowHighlights} ```ts title="src/workflows/delete-wishlist-item.ts" highlights={deleteWishlistItemWorkflowHighlights}
@@ -1536,6 +1585,7 @@ import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-
import { deleteWishlistItemStep } from "./steps/delete-wishlist-item" import { deleteWishlistItemStep } from "./steps/delete-wishlist-item"
import { useQueryGraphStep } from "@medusajs/medusa/core-flows" import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
import { validateItemInWishlistStep } from "./steps/validate-item-in-wishlist" import { validateItemInWishlistStep } from "./steps/validate-item-in-wishlist"
import { validateWishlistExistsStep } from "./steps/validate-wishlist-exists"
type DeleteWishlistItemWorkflowInput = { type DeleteWishlistItemWorkflowInput = {
wishlist_item_id: string wishlist_item_id: string
@@ -1551,9 +1601,10 @@ export const deleteWishlistItemWorkflow = createWorkflow(
filters: { filters: {
customer_id: input.customer_id, customer_id: input.customer_id,
}, },
options: { })
throwIfKeyNotFound: true,
}, validateWishlistExistsStep({
wishlists
}) })
validateItemInWishlistStep({ validateItemInWishlistStep({
@@ -1568,7 +1619,7 @@ export const deleteWishlistItemWorkflow = createWorkflow(
entity: "wishlist", entity: "wishlist",
fields: ["*", "items.*", "items.product_variant.*"], fields: ["*", "items.*", "items.product_variant.*"],
filters: { filters: {
id: wishlists[0].wishlist.id, id: wishlists[0].id,
}, },
}).config({ name: "refetch-wishlist" }) }).config({ name: "refetch-wishlist" })
+1 -1
View File
@@ -5861,7 +5861,7 @@ export const generatedEditDates = {
"references/core_flows/types/core_flows.ThrowUnlessPaymentCollectionNotePaidInput/page.mdx": "2025-01-17T16:43:25.819Z", "references/core_flows/types/core_flows.ThrowUnlessPaymentCollectionNotePaidInput/page.mdx": "2025-01-17T16:43:25.819Z",
"references/core_flows/types/core_flows.ValidatePaymentsRefundStepInput/page.mdx": "2025-01-17T16:43:26.128Z", "references/core_flows/types/core_flows.ValidatePaymentsRefundStepInput/page.mdx": "2025-01-17T16:43:26.128Z",
"references/core_flows/types/core_flows.ValidateRefundStepInput/page.mdx": "2025-01-17T16:43:26.124Z", "references/core_flows/types/core_flows.ValidateRefundStepInput/page.mdx": "2025-01-17T16:43:26.124Z",
"app/plugins/guides/wishlist/page.mdx": "2025-02-11T14:25:46.734Z", "app/plugins/guides/wishlist/page.mdx": "2025-02-20T17:13:30.276Z",
"app/plugins/page.mdx": "2025-01-22T09:36:37.745Z", "app/plugins/page.mdx": "2025-01-22T09:36:37.745Z",
"app/admin-components/components/data-table/page.mdx": "2025-01-22T16:01:01.279Z", "app/admin-components/components/data-table/page.mdx": "2025-01-22T16:01:01.279Z",
"references/order_models/variables/order_models.Order/page.mdx": "2025-01-27T11:43:58.788Z", "references/order_models/variables/order_models.Order/page.mdx": "2025-01-27T11:43:58.788Z",