enhance order processing to include product images from line item properties or fetch from Shopify API
This commit is contained in:
parent
45812e4773
commit
a95cbb82fa
39
server.js
39
server.js
@ -52,9 +52,25 @@ app.post('/webhooks/orders/create', verifyShopifyWebhook, async (req, res) => {
|
|||||||
const processMessage = `[${new Date().toISOString()}] Processing Order ${orderData.order_number}\n`;
|
const processMessage = `[${new Date().toISOString()}] Processing Order ${orderData.order_number}\n`;
|
||||||
fs.appendFileSync('webhook.log', processMessage);
|
fs.appendFileSync('webhook.log', processMessage);
|
||||||
|
|
||||||
// Fetch product images from Shopify Admin API (requires read_products scope)
|
// Fetch product images (reads from line item properties if passed, otherwise fetches from Shopify Admin API)
|
||||||
if (orderData.line_items && orderData.line_items.length > 0) {
|
if (orderData.line_items && orderData.line_items.length > 0) {
|
||||||
await Promise.all(orderData.line_items.map(async (item) => {
|
await Promise.all(orderData.line_items.map(async (item) => {
|
||||||
|
// Check if the image was passed as a line item property from checkout
|
||||||
|
if (item.properties) {
|
||||||
|
if (Array.isArray(item.properties)) {
|
||||||
|
const imgProp = item.properties.find(p => p.name === '_product_image');
|
||||||
|
if (imgProp && imgProp.value) {
|
||||||
|
item.image_url = imgProp.value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (typeof item.properties === 'object') {
|
||||||
|
if (item.properties['_product_image']) {
|
||||||
|
item.image_url = item.properties['_product_image'];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!item.product_id) return;
|
if (!item.product_id) return;
|
||||||
try {
|
try {
|
||||||
const shopifyUrl = `https://${process.env.SHOPIFY_STORE_DOMAIN}/admin/api/2024-01/products/${item.product_id}.json`;
|
const shopifyUrl = `https://${process.env.SHOPIFY_STORE_DOMAIN}/admin/api/2024-01/products/${item.product_id}.json`;
|
||||||
@ -125,11 +141,22 @@ app.post('/create-order', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { customerData, cartItems } = req.body;
|
const { customerData, cartItems } = req.body;
|
||||||
|
|
||||||
const line_items = cartItems.map(item => ({
|
const line_items = cartItems.map(item => {
|
||||||
variant_id: item.variant_id || item.id,
|
const properties = [];
|
||||||
quantity: item.quantity,
|
const imgUrl = item.image || (item.featured_image && (item.featured_image.url || item.featured_image));
|
||||||
price: (item.price / 100).toFixed(2)
|
if (imgUrl && typeof imgUrl === 'string') {
|
||||||
}));
|
properties.push({
|
||||||
|
name: '_product_image',
|
||||||
|
value: imgUrl
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
variant_id: item.variant_id || item.id,
|
||||||
|
quantity: item.quantity,
|
||||||
|
price: (item.price / 100).toFixed(2),
|
||||||
|
properties: properties
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const orderPayload = {
|
const orderPayload = {
|
||||||
order: {
|
order: {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user