I am having a hard time figuring how to filter and update the API on the filtered orders:
In the Shopify orders panel, I did an API call and got a list of all orders. I achieve to only update 1 array with the put method but it’s not dynamic. So basically what i am trying to do is :
Btw this is all javascript
GET api call for all orders then filter the orders who have BOTH country Canada and blank phone number then on those orders with canada and blank number, i tried to update them with PUT method to change the number to "dummy number" but can’t apply that on only those orders with canada and blank number. I did get all the orders and i got array of objects of 6 order. This is my code so far.
$(document).ready(function () {
// this is valid url and returns all orders
var api_url = "https://($api-domain-bla-bla)/admin/api/2020-10/orders.json?status=any.json";
$.ajax({
url: api_url,
contentType: "application/json",
dataType: "json",
success: function (result) {
console.log(result);
// get all orders phone country
let orders = result.orders;
console.log(orders);
for(let i = 0; i < orders.length;i++) {
let phone = orders[i].shipping_address.phone;
let country = orders[i].shipping_address.country;
// here i am trying to filter them right away but don't know how to make array of filter objects
// the if statement works
if ( country === "Canada" && phone === '') {
// over here i am trying to do something to those objects which if statement is true.
let filteredOrder = orders[i].concat
console.log(orderId);
// a function with parameter of the filtere objects variable, to the api PUT method to update
// the filter orders/objects
checkCountry(filteredOrder);
}
// console.log(phone);
// console.log(country);
// checkPhone(phone, country);
}
},
});
});
this is what I get from console.log(result)
https://prnt.sc/vlsh55 (api from all orders)
function checkCountry(order) {
// here i want to update the orders with canada and blank phone number, to put "00000" on the phone
//number i had code here with some if statements but i figured it wouldn't work so i am trying new way
//to get it to work
var api_url_post = "https://($api-domain-bla-bla)/admin/api/2020-10/orders.json?status=any.json";
// this phone variable the structure is the same with the one i get from the call above, don't know
// why it doesn't accept it.
var phone = {
"orders":
{
// "id": 1843613401136,
// "phone": "+0000000000",
"shipping_address": {
"phone": "0000000000"
}
},
}
$.ajax({
method: "PUT",
url: api_url_post,
contentType: "application/json",
crossDomain: true,
dataType: "json",
data: JSON.stringify(phone),
success: function (data, status, jqXHR) {
alert("success");// write success in " "
},
error: function (jqXHR, status) {
// error handler
console.log(jqXHR);
alert('fail' + status.code);
}
});
}
Sorry if I made it unclear, any help?
4
Answers
This is my code so far, tried to put all orders to data like u said still same error at put request
https://prnt.sc/vm3edm
Also, I figured that u can get specific order by id
So mby i get the filtered order id and put in parameter on url and update that order with phone "00000". Should this work?
I am not sure what you are trying to accomplish. My understanding is this:
I dont know much about how the api for getting/putting orders is written, so I what I have in mind may not be efficient. I would map over all the orders and change the Canadian orders without a phone number
And instead of putting the filteredOrders just put all of them
There may be a way to selective update each order that fits your criteria but I dont know about the api you are using
Yeah it worked this way
Thank you so much, this piece of code helped me, i am gratefull ! ๐
UPDATE: Is there away to only get the first order from the array?
I need it like this so if the store has 20k orders so it doesn’t check all orders. only the first one ( the newest one that is made )