updating inventory quantity with PUT returns error - shopify

Here is the body
{
"product": {
"id": 1202316036,
"title": "cricket bat for sale",
"variants":[
{
"inventory_quantity": 500
}
]
}
}
This returns the following error
{
"errors": {
"base": [
"The variant 'Default Title' already exists."
]
}
}
But where as the updating the title seems to be working fine. Here is the body
{
"product": {
"id": 1202316036,
"title": "cricket bat for sale"
}
}
I am sure that PUT header(Content-Type: application/json) are set properly. Because updating title does work. How should I go about updating inventory management
ps: I am using POSTMAN for using shopify API

So you do need the variant id otherwise Shopify thinks you are creating a new variant. Also your variants need to have been set up for Shopify to manage their inventory. e.g.
var product = {
product:{
id: productId,
variants: [
{
id:5991257025,
inventory_management : "shopify",
inventory_quantity:20
},
{
id:5991257089,
inventory_management : "shopify",
inventory_quantity:26
}
]
}
};

I'm not sure if you can do multiple variants per call but to update a single variant's inventory quantity you'd do it like:
var payload = JSON.stringify({
variant: {
id: variantId,
inventory_quantity: qty
}
});
and then put that to "https://myshopifydomain/admin/variants/" +variantId + ".json";
possibly all you need to do is add the variant id for each variant you are updating. Your variant ids can be gotten by GETting the json for your items.

Related

Shopify add cart properties. How do this properties work? It disappears after page refresh

I am setting properties when adding to cart.
Ex:
var formdata=[
"items":{
id:123456,
quantity:1
properties:{'flag':true}
}
];
added using api : /cart/add.js
Details I get from cart.js without refresh
response from : /cart/add.js and cart.js
[
{
"id": 32423423423423,
"properties": {
"flag": true
},
"quantity": 1,
"variant_id": 42705234345345,
}
]
The above items get added successfully to cart. After adding I again fetch the cart details and It has this properties value.But when I refresh the page cart items properties does not have any value.
Ex Currently I am getting this only when page is refreshed:
response from cart.js after page refresh
properties:{
Ref: 0
}
What this properties is?
Why is this happening? If worked, will this properties be available on order create webhook? It only disappears when refreshed. Moreover main reason to add this properties is to receive this properties in order-create webhook to distinguish from normal order. If anyone having other alternative please suggest.
Adding a product to the cart like so:
fetch('/cart/add.js', {
method: "post",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({
items: [
{
quantity: 1,
id: 33116507373620,
properties: {
'flag': true
}
}
]
})
})
And getting the cart.js like so:
fetch('/cart/add.js').then(res => res.json()).then(res => console.log(res))
Will get you result like so:
{
...
"items": [
{
"id": 33116507373620,
"properties": {
"flag": true
},
"quantity": 1,
...
}
],
...
}
From there on what you are doing to not get this result I'm not sure, since this is working/tested and there is no issue.
Please double check if you are targeting the correct object once you get the cart.js response. (there is no properties direct object, it's under items[0].properties in this case)

GraphQL query - Query by ID

I have installed the strapi-starter-blog locally and I'm trying to understand how I can query article by ID (or slug). When I open the GraphQL Playground, I can get all the article using:
query Articles {
articles {
id
title
content
image {
url
}
category {
name
}
}
}
The response is:
{
"data": {
"articles": [
{
"id": "1",
"title": "Thanks for giving this Starter a try!",
"content": "\n# Thanks\n\nWe hope that this starter will make you want to discover Strapi in more details.\n\n## Features\n\n- 2 Content types: Article, Category\n- Permissions set to 'true' for article and category\n- 2 Created Articles\n- 3 Created categories\n- Responsive design using UIkit\n\n## Pages\n\n- \"/\" display every articles\n- \"/article/:id\" display one article\n- \"/category/:id\" display articles depending on the category",
"image": {
"url": "/uploads/blog_header_network_7858ad4701.jpg"
},
"category": {
"name": "news"
}
},
{
"id": "2",
"title": "Enjoy!",
"content": "Have fun!",
"image": {
"url": "/uploads/blog_header_balloon_32675098cf.jpg"
},
"category": {
"name": "trends"
}
}
]
}
}
But when I try to get the article using the ID with variable, like here github code in the GraphQL Playground with the following
Query:
query Articles($id: ID!) {
articles(id: $id) {
id
title
content
image {
url
}
category {
name
}
}
}
Variables:
{
"id": 1
}
I get an error:
...
"message": "Unknown argument \"id\" on field \"articles\" of type \"Query\"."
...
What is the difference and why can't I get the data like in the example of the Github repo.
Thanks for your help.
It's the difference between articles and article as the query. If you use the singular one you can use the ID as argument

In Shopify add a product via SKU rather than Variant ID

I know in Shopify you can add a product to cart with the variant ID like this:
/cart/add?id=VARIANT_ID
I'm trying to find a way to add it by SKU instead, something like this:
/cart/add?sku=SKU
That would be much easier, our SKU's never change.
If that's not possible is there a way to discover the variant id based on SKU?
You always buy a variant in Shopify.
The order itself keeps a record to the variant ID as a reference point.
So the short answer is NO - you can't add a product via a SKU.
For you second question:
is there a way to discover the variant id based on SKU?
Yes but it really depends how are you implementing the SKU part.
If you are on the product page and you have the SKU then you can get the variant_id if you filter out the variants.
But if you are not on the product page and you have for example 500+ products, then the fastest way will be to use GraphQL like so:
{
productVariants(first: 1, query: "sku:SOMESKU"){
edges {
node {
id
}
}
}
}
Where it will return a result such as:
{
"data": {
"productVariants": {
"edges": [
{
"node": {
"id": "gid://shopify/ProductVariant/14726421905460"
}
}
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 3,
"actualQueryCost": 3,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 997,
"restoreRate": 50
}
}
}
}
And you will pass the returned variant ID to the cart instead.

Ember-data hasMany relationship not working (JSONAPIAdapter)

I'm using Ember-Data v.1.13.9 with Ember-CLI v.1.13.8. I'm using the JSONAPIAdapter adapter.
I have a problem with a hasMany relationship. I can see from the Ember inspector that both the main record and the related record are being loaded into the store. However, the relationship doesn't seem to be there since I cannot access the related records details in my template.
models/invoice.js
export default DS.Model.extend(
{
invNum : DS.attr('string'),
created : DS.attr('date', {defaultValue: function() { return new Date(); }}),
clientId : DS.attr('number'),
userId : DS.attr('number'),
details : DS.hasMany('invoice-detail', {async : true}),
});
models/invoice-detail.js
export default DS.Model.extend(
{
invoice : DS.belongsTo('invoice', {async : true}),
detail : DS.attr('string'),
amount : DS.attr('number'),
vat : DS.attr('number'),
});
my JSON data: (URL: /accounts/invoices/1)
{
"data": {
"id": 1,
"attributes": {
"inv-num": "A0011000001",
"created": "November, 01 2000 00:00:00",
"user-id": 2,
"client-id": 14,
"relationships": {
"details": {
"data": [
{
"id": 1,
"type": "invoice-detail"
}
]
}
}
},
"type": "invoice"
},
"included": [
{
"id": 1,
"attributes": {
"amount": 3000,
"detail": "Stage 1 delivery of 3Com Reseller Locator to\r\nFoundation Network LTD",
"vat": 525
},
"type": "invoice-detail"
}
]
}
I've tried accessing the details related array directly:
{{#each model.details as |detail index|}}
{{index}} : {{detail.detail}} £{{detail.amount}} (£{{detail.vat}} vat)
{{/each}}
And by using a controller: invoice/controller.js
export default Ember.Controller.extend({
invoiceDetails : function()
{
var invoice = this.get("model");
var details = invoice.get("details");
Ember.Logger.log("invoiceDetails",invoice,details);
return details;
}.property('model.details'),
});
and
{{#each invoiceDetails as |detail index|}}
{{index}} : {{detail.detail}} £{{detail.amount}} (£{{detail.vat}} vat)
{{/each}}
Neither is providing me with the data that I require.
What am I doing wrong?
A second related issue I am having is that I can't get it to reload data from the server. the {reload:true} makes no difference. Looking at the network traffic I can see that no call to the server is made for second and subsequent visits to this route.
invoice/route.js
export default Ember.Route.extend({
model: function(params) {
return this.store.findRecord('invoice', params.id, { reload: true });
}
})
What I actually want to do here is have one route which retrieves a list of invoices (without the details part - so it's quick to retrieve since I do not need the details on the list page). Then, when I drill down to a specific invoice, make a call to the server to get the full details for that invoice. My plan was to use the shouldReloadRecord function to check if I have details attached to this record or not. If so, use the copy from the store, if not, go to the server and then overwrite the limited "list" record I got initially. As a stepping stone in that direction I figured that just setting {reload:true} in the route would force all requests to go back to the server.
I guess I've misunderstood something somewhere...?

Raven DB HTTP API - Property Traversal

I am given the following JSON structure:
{
"document": {
"sections": {
"x": {
"title": "foo"
},
"y": {
"title": "bar"
}
}
}
}
How do I update value of the title property for a given section using the HTTP API?
I would like to provide a path (string) to get to the property.
This was fixed in build 2254. You should now be able to issue a single scripted patch like this:
EVAL http://localhost:8080/docs/foos/1
{Script:"this.document.sections.x.title = newTitle;",Values:{"newTitle":"Whatever"}}