Shopify Cart
The cart is an example of a global service which multiple components in a theme might need to access. In SALVO-TS, this is represented via the Cart class and it's cart instance (defined in src/theme/Cart.ts).
You can access the cart instance by importing it.
tsimport {cart } from '@/theme/Cart';console .log (cart .items [0]);// { id: 12345678, title: "...", price: 12.23, ... }
tsimport {cart } from '@/theme/Cart';console .log (cart .items [0]);// { id: 12345678, title: "...", price: 12.23, ... }
Quick reference
The following actions are available on the default cart instance:
| Access items | |
|---|---|
| Get all items |
|
| Get item by line |
|
| Get item by SKU |
|
| Get item by product or variant |
|
| Get item by properties |
|
| Get by multiple conditions |
| Update items |
| Add item by variant ID |
|
| Add multiple items |
|
| Update item quantity and attributes |
|
| Remove item |
| Cart attributes |
| Get cart attributes |
|
| Update cart attributes |
| Cart note |
| Get cart note |
|
| Update cart note |
|
Extending the cart interface
You can extend the cart, for example to add new utility functions, by editing the src/theme/Cart.ts file.
- Cart.ts
- Usage
src/theme/Cart.tstsimport {BaseCart ,CartItem ,ShopifyCartItemInput } from '@eastsideco/salvo-ts';classCart extendsBaseCart {/*** Adds an item to the cart and marks it as a gift.* The gift message will be attached as the _gift_message attribute.*/asyncaddGiftItem (variantId : number,giftMessage : string,opts :ShopifyCartItemInput ) {return await this.addItems ([{...opts ,id :variantId ,properties : {...opts .properties ,_gift_message :giftMessage ,}},]);}}export constcart = newCart ();
src/theme/Cart.tstsimport {BaseCart ,CartItem ,ShopifyCartItemInput } from '@eastsideco/salvo-ts';classCart extendsBaseCart {/*** Adds an item to the cart and marks it as a gift.* The gift message will be attached as the _gift_message attribute.*/asyncaddGiftItem (variantId : number,giftMessage : string,opts :ShopifyCartItemInput ) {return await this.addItems ([{...opts ,id :variantId ,properties : {...opts .properties ,_gift_message :giftMessage ,}},]);}}export constcart = newCart ();
tsimport {cart } from './theme/Cart';awaitcart .addGiftItem (123456789, 'Enjoy!', {quantity : 2 });
tsimport {cart } from './theme/Cart';awaitcart .addGiftItem (123456789, 'Enjoy!', {quantity : 2 });
The methods you add to Cart will be available globally on the cart object. You can also add your own custom state and events here, for instance to keep track of the open-state of a side-cart drawer or popover.
Extending queryItems
You can extend the queryItems method by calling addQueryType, and augmenting the CartItemQuery interface:
- Cart.ts
- Usage
src/theme/Cart.tstsimport {BaseCart ,CartItem ,CartItemQuery } from '@eastsideco/salvo-ts';declare module '@eastsideco/salvo-ts' {interfaceCartItemQuery {/** Match items with a variant SKU starting with a substring */skuPrefix : string;}}export default classCart extendsBaseCart {constructor() {super();// Add a new query option which will match items based on a SKU prefixthis.addQueryType ('skuPrefix', (items :CartItem [],prefix : string) => {returnitems .filter ((item :CartItem ) =>item .sku .endsWith (prefix ));});}}export constcart = newCart ();
src/theme/Cart.tstsimport {BaseCart ,CartItem ,CartItemQuery } from '@eastsideco/salvo-ts';declare module '@eastsideco/salvo-ts' {interfaceCartItemQuery {/** Match items with a variant SKU starting with a substring */skuPrefix : string;}}export default classCart extendsBaseCart {constructor() {super();// Add a new query option which will match items based on a SKU prefixthis.addQueryType ('skuPrefix', (items :CartItem [],prefix : string) => {returnitems .filter ((item :CartItem ) =>item .sku .endsWith (prefix ));});}}export constcart = newCart ();
src/example.tstsimport {cart } from './theme/Cart';constitem =cart .items [0];constbulkItems =cart .queryItems ({skuPrefix : '-PALLET' });
src/example.tstsimport {cart } from './theme/Cart';constitem =cart .items [0];constbulkItems =cart .queryItems ({skuPrefix : '-PALLET' });
Handling external updates
In most cases, SALVO-TS will automatically detect when third parties (i.e. apps) modify the cart and will automatically reload the cart state.
If this fails for some reason, you can manually notify the cart by calling notifyCartUpdatedExternally().
src/components/WishlistExample.tsts@Component export classWishlistExample extendsThemeComponent {asyncaddWishlistItemToCart () {// Trigger some cart change in a third party app which can't be picked up automaticallyconstwishlistItem =window .thirdPartyWishlistApp .items [0];awaitwindow .thirdPartyWishlistApp .moveItemToCart (wishlistItem );// Manually notify the theme to pick up the cart changesawaitcart .notifyCartUpdatedExternally ();}}
src/components/WishlistExample.tsts@Component export classWishlistExample extendsThemeComponent {asyncaddWishlistItemToCart () {// Trigger some cart change in a third party app which can't be picked up automaticallyconstwishlistItem =window .thirdPartyWishlistApp .items [0];awaitwindow .thirdPartyWishlistApp .moveItemToCart (wishlistItem );// Manually notify the theme to pick up the cart changesawaitcart .notifyCartUpdatedExternally ();}}