I am struggling to find a way to filter and update the API on the filtered orders:
While working in the Shopify orders panel, I made an API call to retrieve a list of all orders. I managed to update one array using the put method, but it's not dynamic. Essentially, what I'm attempting to do is:
By the way, this is all written in JavaScript.
First, make a GET api call for all orders, then filter out the orders that have BOTH Canada as the country and a blank phone number. After isolating those specific orders, which meet the criteria of being from Canada with a blank phone number, I attempted to use the PUT method to change the phone number to "dummy number." However, I encountered difficulty applying this operation solely to those orders meeting the specified conditions. Initially, I retrieved all the orders, resulting in an array of 6 order objects. Below is my current code:
$(document).ready(function () {
// Valid URL that 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;
// Filtering them right away, but unsure how to create an array of filtered objects.
// The if statement functions correctly
if ( country === "Canada" && phone === '') {
// Perform actions on these objects where the if statement holds true.
let filteredOrder = orders[i].concat
console.log(orderId);
// A function with parameters of the filtered objects variable and the API PUT method to update
// the filtered orders/objects
checkCountry(filteredOrder);
}
}
},
});
});
Results from console.log(result)
(API response with all orders)
function checkCountry(order) {
// Here I aim to update the orders with Canadian addresses and blank phone numbers by setting the phone
// number to "00000"
var api_url_post = "https://($api-domain-bla-bla)/admin/api/2020-10/orders.json?status=any.json";
// This phone object has a similar structure to what I received from the earlier call, not sure
// why it's not being accepted.
var phone = {
"orders":
{
"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);
}
});
}
Apologies if my explanation is unclear, any assistance would be appreciated?