Hi guys,
I am currently working on building an order tracking page using the Shopify Origin theme. I have implemented all the necessary components and UI elements. However, I am facing some difficulties in connecting with the API to retrieve and display the actual order status.
If you have experience with Shopify and API integrations, I kindly request your assistance in resolving this issue. Your expertise and guidance would be greatly appreciated. Thank you in advance for your help!
URL: https://basicbastard.co/pages/notes
I used the below code:
<div class="order-tracking">
<div class="input-container">
<input type="text" id="order_number" class="order-input" placeholder="Order number">
<button class="order-button" onclick="trackOrder()">TRACK</button>
</div>
<div id="order_status"></div>
</div>
<style>
.order-tracking {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
}
.input-container {
display: flex;
align-items: center;
}
.order-tracking input.order-input {
flex-grow: 1;
text-align: left;
padding: 1.5rem;
margin: var(--inputs-border-width);
transition: box-shadow var(--duration-short) ease, border-color 0.3s; /* Added transition for border color */
appearance: none;
background-color: rgb(var(--color-background));
color: rgb(var(--color-foreground));
font-size: 13px;
width: 80%;
box-sizing: border-box;
border-radius: var(--inputs-radius);
height: 4rem;
min-height: calc(var(--inputs-border-width) * 2);
min-width: calc(7rem + (var(--inputs-border-width) * 2));
position: relative;
border: 0;
border: 1px solid lightgrey;
margin-bottom: 13px;
box-shadow: none; /* Remove the box shadow */
}
.order-tracking input.order-input:focus,
.order-tracking input.order-input:hover {
outline: none;
border: 2px solid grey; /* Set the border color to 2px solid grey on hover */
box-shadow: none; /* Remove the box shadow */
}
.order-tracking input.order-input:focus {
border-color: #282F3D; /* Set the border color to #282F3D on focus */
}
.order-tracking button.order-button {
background-color: #282F3D;
color: #faf9f6;
padding: 10px 20px;
border: none;
border-radius: 0;
cursor: pointer;
width: 30%;
text-align: center;
font-size: 11px;
font-weight: 600;
margin-left: 2px; /* Added margin to separate the button from the input field */
height: 4rem; /* Set the height of the button to match the input field */
margin-bottom: 12px;
}
.order-tracking #order_status {
margin-top: 7px;
font-weight: bold;
font-size: 11px !important;
}
</style>
<script>
function trackOrder() {
var orderNumber = document.getElementById("order_number").value;
var orderStatusDiv = document.getElementById("order_status");
fetchOrderStatus(orderNumber)
.then(function (status) {
orderStatusDiv.innerText = "Order #" + orderNumber + ": " + status;
})
.catch(function (error) {
orderStatusDiv.innerText = "Oops! Something went wrong.";
});
}
function fetchOrderStatus(orderNumber) {
return new Promise(function (resolve, reject) {
fetch("/admin/api/2021-09/orders.json?name=" + orderNumber)
.then(function (response) {
if (response.ok) {
return response.json();
} else {
throw new Error("Oops! Something went wrong.");
}
})
.then(function (data) {
var order = data.orders[0];
var status = order.financial_status;
if (status) {
resolve(status);
} else {
throw new Error("Oops! Something went wrong.");
}
})
.catch(function (error) {
reject(error);
});
});
}
</script>