Skip to content

Commit

Permalink
[checkout] Add cart instructions API
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesvidler committed Jun 28, 2024
1 parent 02d7e2c commit 321749e
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changeset/heavy-adults-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@shopify/ui-extensions-react': minor
'@shopify/ui-extensions': minor
---

Added `CartInstructions` (accessed using `api.instructions`) to checkout. These represent the cart instructions used to create the checkout and possibly limit extension capabilities. These instructions should be checked prior to performing any actions that may be affected by them.

For example, if you intend to add a discount code via the `applyDiscountCodeChange` method, check `api.instructions.discounts.canUpdateDiscountCodes` to ensure it's supported in this checkout.
1 change: 1 addition & 0 deletions packages/ui-extensions-react/src/surfaces/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,4 @@ export {useDeliveryGroupListTarget} from './checkout/hooks/delivery-group-list-t
export {useDeliverySelectionGroups} from './checkout/hooks/delivery-selection-groups';
export {useCheckoutToken} from './checkout/hooks/checkout-token';
export {useCustomerPrivacy} from './checkout/hooks/customer-privacy';
export {useInstructions} from './checkout/hooks/instructions';
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {
CartInstructions,
RenderExtensionTarget,
} from '@shopify/ui-extensions/checkout';

import {useApi} from './api';
import {useSubscription} from './subscription';

/**
* Returns the cart instructions used to create the checkout and possibly limit extension capabilities.
*/
export function useInstructions<
Target extends RenderExtensionTarget = RenderExtensionTarget,
>(): CartInstructions {
return useSubscription(useApi<Target>().instructions);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/ui-extensions/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type ApiVersion =
| '2023-10'
| '2024-01'
| '2024-04'
| '2024-07'
| 'unstable';

/**
Expand Down Expand Up @@ -865,6 +866,7 @@ export type StorefrontApiVersion =
| '2023-07'
| '2024-01'
| '2024-04'
| '2024-07'
| 'unstable';

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/ui-extensions/src/surfaces/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export type {
VisitorError,
VisitorSuccess,
VisitorResult,
CartInstructions,
AttributesCartInstructions,
DeliveryCartInstructions,
DiscountsCartInstructions,
CartLinesCartInstructions,
MetafieldsCartInstructions,
NotesCartInstructions,
} from './checkout/api/standard/standard';

export type {
Expand Down
3 changes: 3 additions & 0 deletions packages/ui-extensions/src/surfaces/checkout/api/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,8 @@ export interface MailingAddress {
}

export interface ShippingAddress extends MailingAddress {
/**
* Specifies whether the address should be saved to the buyer's account.
*/
oneTimeUse?: boolean;
}
103 changes: 102 additions & 1 deletion packages/ui-extensions/src/surfaces/checkout/api/standard/standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface Extension<Target extends ExtensionTarget = ExtensionTarget> {
/**
* The API version that was set in the extension config file.
*
* @example '2023-07', '2023-10', '2024-01', '2024-04', 'unstable'
* @example '2023-07', '2023-10', '2024-01', '2024-04', '2024-07', 'unstable'
*/
apiVersion: ApiVersion;

Expand Down Expand Up @@ -490,6 +490,16 @@ export interface StandardApi<Target extends ExtensionTarget = ExtensionTarget> {
*/
appliedGiftCards: StatefulRemoteSubscribable<AppliedGiftCard[]>;

/**
* The cart instructions used to create the checkout and possibly limit extension capabilities.
*
* These instructions should be checked prior to performing any actions that may be affected by them.
*
* For example, if you intend to add a discount code via the `applyDiscountCodeChange` method,
* check `discounts.canUpdateDiscountCodes` to ensure it's supported in this checkout.
*/
instructions: StatefulRemoteSubscribable<CartInstructions>;

/**
* The metafields requested in the
* [`shopify.extension.toml`](https://shopify.dev/docs/api/checkout-ui-extensions/configuration)
Expand Down Expand Up @@ -1906,3 +1916,94 @@ export interface TrackingConsentChangeResultError {
*/
message: string;
}

export interface CartInstructions {
/**
* Cart instructions related to cart attributes.
*/
attributes: AttributesCartInstructions;

/**
* Cart instructions related to delivery.
*/
delivery: DeliveryCartInstructions;

/**
* Cart instructions related to discounts.
*/
discounts: DiscountsCartInstructions;

/**
* Cart instructions related to cart lines.
*/
lines: CartLinesCartInstructions;

/**
* Cart instructions related to metafields.
*/
metafields: MetafieldsCartInstructions;

/**
* Cart instructions related to notes.
*/
notes: NotesCartInstructions;
}

export interface AttributesCartInstructions {
/**
* Indicates whether or not cart attributes can be updated.
*/
canUpdateAttributes: boolean;
}

export interface DeliveryCartInstructions {
/**
* Indicates whether a buyer can select a custom address.
*
* When true, this implies extensions can update the delivery address.
*/
canSelectCustomAddress: boolean;
}

export interface DiscountsCartInstructions {
/**
* Indicates whether or not discount codes can be updated.
*/
canUpdateDiscountCodes: boolean;
}

export interface CartLinesCartInstructions {
/**
* Indicates whether or not new cart lines can be added.
*/
canAddCartLine: boolean;

/**
* Indicates whether or not cart lines can be removed.
*/
canRemoveCartLine: boolean;

/**
* Indicates whether or not cart lines can be updated.
*/
canUpdateCartLine: boolean;
}

export interface MetafieldsCartInstructions {
/**
* Indicates whether or not cart metafields can be added or updated.
*/
canSetCartMetafields: boolean;

/**
* Indicates whether or not cart metafields can be deleted.
*/
canDeleteCartMetafield: boolean;
}

export interface NotesCartInstructions {
/**
* Indicates whether or not notes can be updated.
*/
canUpdateNote: boolean;
}
1 change: 1 addition & 0 deletions packages/ui-extensions/src/surfaces/checkout/targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ export interface CustomerAccountStandardApi<
| 'extension'
| 'extensionPoint'
| 'i18n'
| 'instructions'
| 'lines'
| 'localization'
| 'metafields'
Expand Down

0 comments on commit 321749e

Please sign in to comment.