Shopify / js-buy-sdk

The JS Buy SDK is a lightweight library that allows you to build ecommerce into any website. It is based on Shopify's API and provides the ability to retrieve products and collections from your shop, add products to a cart, and checkout.
https://shopify.github.io/js-buy-sdk
MIT License
987 stars 262 forks source link

Add Carts #979

Closed scottdixon closed 7 months ago

scottdixon commented 7 months ago

In preparation for Checkout API deprecation, this PR adds Carts.

This is my first contribution to the library, please let me know what can be improved :)

Creating a Cart

const input = {
  lines: {
    merchandiseId: 'gid://shopify/ProductVariant/13666012889144',
    quantity: 5,
    attributes: [{key: "MyKey", value: "MyValue"}]
  },
  note: 'This is a cart note!'
];

// Create a cart
client.cart.create(input).then((cart) => {
  // Do something with the cart
});

Fetching a Cart

const cartId = 'gid://shopify/Cart/Z2NwLWV1cm9wZS13ZXN0NDowMUhOTTI0QVZYV1NOSEVNOUVCQ1JSNUhSVg'

client.cart.fetch(cartId).then((cart) => {
  // Do something with the cart
});

Updating Cart Attributes

const cartId = 'gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSE5WWTAyVjlETjFDNVowVFZEWVMwMVJR';
const attributes = [{key: "MyKey", value: "MyValue"}];

client.cart.updateAttributes(cartId, attributes).then((cart) => {
  // Do something with the updated cart
});

Updating Buyer Identity

const cartId = 'gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSE5WWTAyVjlETjFDNVowVFZEWVMwMVJR';
const buyerIdentity = {email: "hello@hi.com"};

client.cart.updateBuyerIdentity(cartId, buyerIdentity).then((cart) => {
  // Do something with the updated cart
});

Updating Discount Codes

const cartId = 'gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSE5WWTAyVjlETjFDNVowVFZEWVMwMVJR';
const discountCodes = [{code: "MyCode"}];

client.cart.updateDiscountCodes(cartId, discountCodes).then((cart) => {
  // Do something with the updated cart
});

Adding Cart Line Items

const cartId = 'gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSE5WWTAyVjlETjFDNVowVFZEWVMwMVJR';
const lines = [{merchandiseId: 'gid://shopify/Product/123456', quantity: 5}];

client.cart.addLineItems(cartId, lines).then((cart) => {
  // Do something with the updated cart
});

Removing Cart Line Items

const cartId = 'gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSE5WWTAyVjlETjFDNVowVFZEWVMwMVJR';
const lineIdsToRemove = ['gid://shopify/CartLineItem/123456'];

client.cart.addLineItems(cartId, lineIdsToRemove).then((cart) => {
  // Do something with the updated cart
});

Updating Cart Line Items

const cartId = 'gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSE5WWTAyVjlETjFDNVowVFZEWVMwMVJR';
const lines = [{id: 'gid://shopify/CartLineItem/123456', quantity: 5}];

client.cart.updateLineItems(cartId, lines).then((cart) => {
  // Do something with the updated cart
});

Updating Cart Notes

const cartId = 'gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSE5WWTAyVjlETjFDNVowVFZEWVMwMVJR';
const note = 'A note for the cart';

client.cart.updateNote(cartId, note).then((cart) => {
  // Do something with the updated cart
}
benjaminsehl commented 7 months ago

Closing this for now pending further discussion.

tonyyb commented 3 months ago

Hi,

This PR was closed without more details but can you tell us if an implementation is scheduled in this library please ?

Thank you !

aoliverio-ev commented 2 months ago

Hey @benjaminsehl are there any updates as to if/when this work will be resumed? TY

AllanPooley commented 11 hours ago

Hey @scottdixon thank you for your work on this 🙏 @benjaminsehl has there since been an update?