php - How to add items to the cart using the Shopify API -


i'm working on custom web app using shopify api. idea here use app exclusive storefront , make requests shopify api. i've set private application in shopify account this.

i have no problem pulling in products api (using /admin/products.json api endpoint). i'm having heck of time trying product added cart domain using ajax api , /cart/add.js api endpoint.i proper json response when using rest client (i'm using google chrome extension postman), can't life of me work within own app.

in app, i'm sending ajax request own server, in turn calls api endpoint using curl , returns json response(to avoid cross-domain problem/having use jsonp). though both json responses identical, item not being added cart in web app.

sample code:

javascript:

$(".add-to-cart").on('click', function() {      // sample initialization code here      // ...         $.post("/url-to-server", {             id: id, // variant id             quantity: 1         }, function(response) {                     // more checking make sure have proper response         });          return false;     }); 

server side code (php):

protected function _ajax_add_item_to_cart() {     $args = $this->input->post();      // custom app method makes curl request     $result = $this->_shopify_request('post', 'cart/add.js', $args);      // custom app method returns valid json     return $this->json_response(true, 'item added!', [         'item' => $result     ]); } 

sample json response (same result when using postman rest client):

{   "id": 313743963,   "title": "all natural gumbits 16oz",   "price": 3800,   "line_price": 3800,   "quantity": 1,   "sku": "",   "grams": 0,   "vendor": "horseshow",   "properties": null,   "variant_id": 313743963,   "url": "/products/all-natural-gumbits-16oz",   "image": "http://cdn.shopify.com/s/files/1/0235/4155/products/gumbits.png?43",   "handle": "all-natural-gumbits-16oz",   "requires_shipping": true } 

this first app i've built using shopify api, i'd appreciate guidance can get. looked signing partner application , using oauth authentication, seems overkill need since don't want use shopify storefront , don't plan on offering app in shopify app store.

there seems undocumented feature (documented here) forward customer directly checkout page (with prefilled cart). need construct special url, example:

# order 1 item of product variant_id 300823433 http://murray-inc9186.myshopify.com/cart/300823433:1 

or

# order 1 item  of product variant_id 300823433 # order 2 items of product variant_id 261826220 http://murray-inc9186.myshopify.com/cart/300823433:1,261826220:2 

maybe can use special url instead of ajax posting cart.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -