Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shopify API - add to basket

I'm thinking of using Shopify to manage my shop. I need to add the products to the website with the API.

http://api.shopify.com/

I can get the products and stocks with this API call :

http://api.shopify.com/product.html#index

But, then I would like to 'Add to basket' - I cannot see from the API docs how to do this. Once added to the basket - I'd like to get the basket contents so I can display something like...

"2 items : £130 - checkout"

I don't need a detailed answer - just point me in the right direction - thanks.

like image 360
Boz Avatar asked Feb 22 '23 13:02

Boz


1 Answers

The Shopify back-end knows nothing about individual user carts, they only exist in browser-land. My mistake, the backend DOES know about carts, but you can't edit them through the REST API. Here's the Cart endpoint if you're interested in getting carts.

To manipulate a user's cart, you'll need to use the Ajax API from the storefront. Specifically, this call will add products to the cart:

http://store.myshopify.com/cart.add.js?quantity=2&id=30104012

which returns json that looks something like this:

{
    "handle": "amelia",
    "line_price": 4000,
    "requires_shipping": true,
    "price": 2000,
    "title": "amelia - medium",
    "url": "/products/amelia",
    "quantity": 2,
    "id": 30104012,
    "grams": 200,
    "sku": "",
    "vendor": "the candi factory",
    "image": "http://static.shopify.com/s/files/1/0040/7092/products/2766315_da1b.png?1268045506",
    "variant_id": 30104012
}

You can get the cart itself using the following call:

http://store.myshopify.com/cart.js

Which will get you this:

{
    "items": [
        {
            "handle": "aquarius",
            "line_price": 6000,
            "requires_shipping": true,
            "price": 2000,
            "title": "aquarius - medium",
            "url": "/products/aquarius",
            "quantity": 3,
            "id": 30104042,
            "grams": 181,
            "sku": "",
            "vendor": "the candi factory",
            "image": "http://static.shopify.com/s/files/1/0040/7092/products/aquarius_1.gif?1268045506",
            "variant_id": 30104042
        },
        {
            "handle": "amelia",
            "line_price": 4000,
            "requires_shipping": true,
            "price": 2000,
            "title": "amelia - medium",
            "url": "/products/amelia",
            "quantity": 2,
            "id": 30104012,
            "grams": 200,
            "sku": "",
            "vendor": "the candi factory",
            "image": "http://static.shopify.com/s/files/1/0040/7092/products/2766315_da1b.png?1268045506",
            "variant_id": 30104012
        }
    ],
    "requires_shipping": true,
    "total_price": 10000,
    "attributes": null,
    "item_count": 5,
    "note": null,
    "total_weight": 947
}
like image 76
David Underwood Avatar answered Feb 27 '23 15:02

David Underwood