All things Shopify and commerce
I have created a custom pixel to send order data on "checkout_complete" to an application i've developed to upload orders to an erp software. I need to get the PO # of the shopify order that was placed. The checkout_complete hook only returns the order id. Can I make a request to the shopify API to get the PO # using the order id before sending a post request to my server?
Below is my code. Including a comment where I'd like to make the request.
analytics.subscribe('checkout_completed', (event) => {
const checkout = event.data.checkout;
const checkoutTotalPrice = checkout.totalPrice?.amount;
const allDiscountCodes = checkout.discountApplications.map((discount) => {
if (discount.type === 'DISCOUNT_CODE') {
return discount.title;
}
});
const firstItem = checkout.lineItems[0];
const items = [];
checkout.lineItems.map((item, index) => {
const newItem = {
sku: item.variant.sku,
quantity: item.quantity,
price: item.finalLinePrice?.amount,
}
items[index] = newItem;
})
/* items.forEach((item) => {
console.log(item);
})*/
const billAdd = {
firstName: checkout.billingAddress.firstName,
lastName: checkout.billingAddress.lastName,
address1: checkout.billingAddress.address1,
address2: checkout.billingAddress.address2,
city: checkout.billingAddress.city,
country: checkout.billingAddress.country,
province: checkout.billingAddress.province,
provinceCode: checkout.billingAddress.provinceCode,
zip: checkout.billingAddress.zip,
phone: checkout.billingAddress.phone,
};
const shipAdd = {
firstName: checkout.shippingAddress.firstName,
lastName: checkout.shippingAddress.lastName,
address1: checkout.shippingAddress.address1,
address2: checkout.shippingAddress.address2,
city: checkout.shippingAddress.city,
country: checkout.shippingAddress.country,
province: checkout.shippingAddress.province,
provinceCode: checkout.shippingAddress.provinceCode,
zip: checkout.shippingAddress.zip,
phone: checkout.shippingAddress.phone,
};
const email = checkout.email;
const orderId = event.data.checkout.order?.id;
/*make api request to shopify using orderId to get the order PO Number. */
const payload = {
event_name: event.name,
event_data: {
orderId,
totalPrice: checkoutTotalPrice,
discountCodesUsed: allDiscountCodes,
billingAddress: billAdd,
shippingAddress: shipAdd,
email: email,
items: items,
},
};
console.log(payload);
fetch('https://******/uploading-api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
keepalive: true,
});
});
Learn how to expand your operations internationally with Shopify Academy’s learning path...
By Shopify Feb 4, 2025Hey Community, happy February! Looking back to January, we kicked off the year with 8....
By JasonH Feb 3, 2025Expand into selling wholesale with Shopify Academy’s learning path, B2B on Shopify: Lau...
By Shopify Jan 28, 2025