-
Notifications
You must be signed in to change notification settings - Fork 50
Add code examples of POS subscriptions UI extension #3550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
janezhu918
wants to merge
1
commit into
2025-10
from
jane/add_code_examples_of_pos_subscriptions_ui_extension
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
...ages/ui-extensions/docs/surfaces/point-of-sale/mdxExamples/subscription-example/Modal.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import {render} from 'preact'; | ||
| import {useState, useEffect} from 'preact/hooks'; | ||
|
|
||
| // [START modal.fetch] | ||
| async function fetchSellingPlans(variantId) { | ||
| const requestBody = { | ||
| query: `#graphql | ||
| query GetSellingPlans($variantId: ID!) { | ||
| productVariant(id: $variantId) { | ||
| sellingPlanGroups(first: 10) { | ||
| nodes { | ||
| name | ||
| sellingPlans(first: 10) { | ||
| nodes { | ||
| id | ||
| name | ||
| category | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `, | ||
| variables: { variantId: `gid://shopify/ProductVariant/${variantId}`}, | ||
| }; | ||
|
|
||
| const res = await fetch('shopify:admin/api/graphql.json', { | ||
| method: 'POST', | ||
| body: JSON.stringify(requestBody), | ||
| }); | ||
| return res.json(); | ||
| } | ||
| // [END modal.fetch] | ||
|
|
||
| export default function extension() { | ||
| render(<Modal />, document.body); | ||
| } | ||
|
|
||
| function Modal() { | ||
| // For this example, we'll just use the first selling plan item | ||
| const sellingPlanItem = shopify.cart.current.value.lineItems | ||
| .find(lineItem => lineItem.hasSellingPlanGroups === true) | ||
|
|
||
| const [response, setResponse] = useState(undefined) | ||
|
|
||
| useEffect(() => { | ||
| async function getSellingPlans() { | ||
| setResponse(await fetchSellingPlans(sellingPlanItem?.variantId)) | ||
| } | ||
| getSellingPlans() | ||
| }, [sellingPlanItem]) | ||
|
|
||
| // [START modal.handle-click] | ||
| const handleClick = (plan) => { | ||
| shopify.cart.addLineItemSellingPlan({ | ||
| lineItemUuid: sellingPlanItem.uuid, | ||
| // convert from GID to ID | ||
| sellingPlanId: Number(plan.id.split('/').pop()), | ||
| sellingPlanName: plan.name, | ||
| }) | ||
| window.close() | ||
| } | ||
| // [END modal.handle-click] | ||
|
|
||
| return ( | ||
| <s-page heading='POS modal'> | ||
| <s-scroll-box> | ||
| <s-box padding="small"> | ||
| {response?.data.productVariant.sellingPlanGroups.nodes.map(group => { | ||
| return ( | ||
| <s-section key={`${group.name}-section`} heading={group.name}> | ||
| {group.sellingPlans.nodes.map(plan => { | ||
| return ( | ||
| <s-clickable key={`${plan.name}-clickable`} onClick={() => { | ||
| handleClick(plan) | ||
| }}> | ||
| <s-text key={`${plan.name}-text`}>{plan.name}</s-text> | ||
| </s-clickable> | ||
| ) | ||
| })} | ||
| </s-section> | ||
| ) | ||
| })} | ||
| </s-box> | ||
| </s-scroll-box> | ||
| </s-page> | ||
| ); | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
packages/ui-extensions/docs/surfaces/point-of-sale/mdxExamples/subscription-example/Tile.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import {render} from 'preact'; | ||
| import {useState} from 'preact/hooks'; | ||
|
|
||
| export default function extension() { | ||
| render(<Tile />, document.body); | ||
| } | ||
|
|
||
| function Tile() { | ||
| const [sellingPlanEligible, setSellingPlanEligible] = useState(false); | ||
|
|
||
| // [START tile.subscribe] | ||
| useEffect(() => { | ||
| const unsubscribe = shopify.cart.current.subscribe(cart => { | ||
| const sellingPlanEligibleLineItems = cart.lineItems.filter(lineItem => lineItem.hasSellingPlanGroups === true) | ||
|
|
||
| setSellingPlanEligible(sellingPlanEligibleLineItems.length > 0) | ||
| }) | ||
| return unsubscribe | ||
| }, []) | ||
| // [END tile.subscribe] | ||
|
|
||
| return ( | ||
| <s-tile | ||
| heading={"Subscriptions"} | ||
| subheading={sellingPlanEligible | ||
| ? "Subscriptions available" | ||
| : "Subscriptions not available" | ||
| } | ||
| // [START tile.disabled] | ||
| disabled={!sellingPlanEligible} | ||
| // [END tile.disabled] | ||
| onClick={() => shopify.action.presentModal()} | ||
| /> | ||
| ); | ||
| } |
15 changes: 15 additions & 0 deletions
15
...sions/docs/surfaces/point-of-sale/mdxExamples/subscription-example/shopify.extension.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| api_version = "2025-10" | ||
|
|
||
| [[extensions]] | ||
| type = "ui_extension" | ||
| name = "Subscription Tutorial" | ||
| handle = "subscription-tutorial" | ||
| description = "POS UI extension subscription tutorial" | ||
|
|
||
| [[extensions.targeting]] | ||
| module = "./src/Tile.jsx" | ||
| target = "pos.home.tile.render" | ||
|
|
||
| [[extensions.targeting]] | ||
| module = "./src/Modal.jsx" | ||
| target = "pos.home.modal.render" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious: What does the
keyprop do in this context?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we're looping through the selling plans here, it's good practice to include a unique key to tell these components apart.