skip to Main Content

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/vls9qq

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


  1. Chosen as BEST ANSWER
        $(document).ready(function () {
      var api_url = "https://api-blalbla/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);
          orders = orders.map(order=>{
            let {country, phone } = order.shipping_address
            if(country === "Canada" && phone === ''){
            order.shipping_address.phone = '0000000'
            checkCountry(order, orders)
                console.log(orders);
                console.log(order);
            }
            else {
              return order
            }
          
            
            
    })
        },
      });
    });
    
    function checkCountry(filteredOrder, allOrders) {
         
        var api_url_post = "https://api-blalbla/admin/api/2020-10/orders.json?status=any.json";
        
    //     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(allOrders),
              success: function (data, status, jqXHR) {
                  alert("success");// write success in " "
              },
      
              error: function (jqXHR, status) {
                  // error handler
                  console.log(jqXHR);
                  alert('fail' + status.code);
              }
           });
                
    }
    

    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

    /admin/api/2020-10/orders/{order_id}.json
    

    So mby i get the filtered order id and put in parameter on url and update that order with phone "00000". Should this work?


  2. I am not sure what you are trying to accomplish. My understanding is this:

    1. You are making an API request to get a list of orders.
    2. You want to filter that list for the orders from canada without a phone number.
    3. You want to edit the filtered list with dummy phone numbers and put these changes to server.

    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

    orders = orders.map(order=>{
      let {country, phone } = order.shipping_address
      if(country === "Canada" && phone === ''){
        order.shipping_address.phone = '0000000'
      }
      return order
    })
    

    And instead of putting the filteredOrders just put all of them

    $.ajax({
              method: "PUT",
              url: api_url_post,
              contentType: "application/json",
              crossDomain: true,
              dataType: "json",
              data: JSON.stringify(orders),
              success: function (data, status, jqXHR) {
                  alert("success");// write success in " "
              },
      
              error: function (jqXHR, status) {
                  // error handler
                  console.log(jqXHR);
                  alert('fail' + status.code);
              }
           });
    

    There may be a way to selective update each order that fits your criteria but I dont know about the api you are using

    Login or Signup to reply.
  3. Yeah it worked this way

    Also, I figured that u can get specific order by id
    
    /admin/api/2020-10/orders/{order_id}.json
    So mby i get the filtered order id and put in parameter on url and update that order with phone "00000". Should this work?
    

    Thank you so much, this piece of code helped me, i am gratefull ! ๐Ÿ™‚

       orders = orders.map(order=>{
      let {country, phone } = order.shipping_address
      if(country === "Canada" && phone === ''){
        order.shipping_address.phone = '0000000'
      }
      return order
    })
    
    Login or Signup to reply.
  4. UPDATE: Is there away to only get the first order from the array?

    // mby like except .map to [0]
    orders = orders[0](order=>{
      let {country, phone } = order.shipping_address
      if(country === "Canada" && phone === ''){
        order.shipping_address.phone = '0000000'
      }
      return order
    })
    

    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 )

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search