skip to Main Content

Looking for a logic possibly using a for loop to loop through orders for an ordering system using Shopify. Order IDs come in sequentially, as they come in we want to assign a pickup location based on the order number.

For example, first 100 orders will have the pickup location set to Location A and next 100 to Location B and so on. But once we reach the 500 order, we want to start again from Location A and then loop through the locations. There will be no end to this loop until the orders stop.

All orders get prepared at a central location and gets dispatched to the corresponding location. OrderID is created once the order is placed on the website so we want the pickup location shown at the final Order Status page.

Please let me know if I can provide clarification.

I can achieve this logic by If statements by static assignment of Order IDs and of course this is not scalable as we don’t know how many orders we will get.

<script>
  var orderId = {{ order_number }};
if (orderId > 100) {
  Shopify.Checkout.OrderStatus.addContentBox(
    '<h3> Pickup Location A</h3>'
)}
 else {
  Shopify.Checkout.OrderStatus.addContentBox(
    '<h2>Pickup Location</h2>'    
  )}
</script>

2

Answers


  1. I think this does what I think you’re asking.

    pickupLocations = ['A','B','C','D','E'];
    pickupGroup = 0;
    orderCounter = 1;
    
    for (orderID=1, orderID < orderIDs.length; orderID++) {
       pickup = pickupLocations[pickupGroup];
       if (orderCounter==100) {
          pickupGroup++;
          orderCounter=0;
       }
    }
    

    If you really want to break out, you can use the “break;” command. Or, to have the loop run only while a condition is met, “while (condition) {…}”.

    Login or Signup to reply.
  2. How about something like this? The first 100 orders go to A, second 100 to B, …, fifth 100 to E then it starts cycling (sixth 100 go to A, seventh 100 go to B, …).

    const orderId = prompt('Enter an order number'); //TODO: set to actual order number
    const pickupLocations = [
    	['<h3>Pickup Location A</h3>', '<img src="https://dummyimage.com/30&text=A"></img>'],
    	['<h3>Pickup Location B</h3>', '<img src="https://dummyimage.com/30&text=B"></img>'],
    	['<h3>Pickup Location C</h3>', '<img src="https://dummyimage.com/30&text=C"></img>'],
    	['<h3>Pickup Location D</h3>', '<img src="https://dummyimage.com/30&text=D"></img>'],
    	['<h3>Pickup Location E</h3>', '<img src="https://dummyimage.com/30&text=E"></img>']
    ];
    const pickupIndex = Math.floor(orderId/100) % pickupLocations.length;
    
    //TODO: comment out console.log() in favor of next line
    console.log(...pickupLocations[pickupIndex]);    
    //Shopify.Checkout.OrderStatus.addContentBox(...pickupLocations[pickupIndex]);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search