Hi Anthony, thanks for the reply again.
I’ve had my code (pasted below) throw an error if orderItem could not be found so orderItem would not have been null. I’ve also doublechecked CMS and via console log that fulfillerId exists and I’m guessing that’s supposed to be my custom fulfiller since after createFulfillment the order does get fulfilled by whatever custom fulfiller I had assigned to the product.
But I had thought sendFulfillment would’ve worked regardless of fulfilment status, similar to how one could manually send the order to assigned fulfillers via Wix Dashboard in the Orders page.
Also, I tried another test order and saw that the “Order is already Fulfilled” was due to the function being called a second time, another thing I’ll have to fix I guess…
I’ll just throw the entire code block here, did not think to include most of it as I didn’t want to clutter the original post with code that might not directly relate to the problem.
export async function tracking_beforeUpdate(item, context) {
console.log("Running")
// Check if item was previously not active
if (item.active && !context.currentItem.active) {
console.log(item)
console.log(context)
// try {
const order = await orders.getOrder(item.orderRef)
// Check if item has yet to be fulfilled before proceeding
if (["NOT_FULFILLED", "PARTIALLY_FULFILLED"].includes(order.fulfillmentStatus)) {
const trackingInfo = {
trackingNumber: item.title, //uuid4 used to generate tracking link
shippingProvider: item.carrier, //Name str of custom shipping provider
trackingLink: domain + item["link-order-tracking-title"],
}
console.log("Order Items:", order.lineItems)
// References to Stores/Products get saved as [product-id]-[variant-id]
const itemProductFragments = item.productRef.split("-")
const itemProductId = itemProductFragments.splice(0, 5).join("-")
const itemVariantId = itemProductFragments.join("-")
console.log(itemProductId, itemVariantId)
const orderItem = order.lineItems.find((lineItem) => lineItem.catalogReference.catalogItemId == itemProductId &&
(lineItem.catalogReference.options?.variantId || "00000000-0000-0000-0000-000000000000") == itemVariantId)
if (!orderItem) {
// Kill automation if orderItem cannot be found
throw new Error("Error getting orderItem")
}
console.log("Item", item)
const lineItems = [{
index: order.lineItems.findIndex((lineItem) => lineItem._id == orderItem._id) + 1
}]
// const currentLineItem = order.lineItems.find((lineItem) => {
// return (lineItem.catalogReference.catalogItemId == itemProductId &&
// (lineItem.catalogReference.options?.variantId || "00000000-0000-0000-0000-000000000000") == itemVariantId)
// })
// if (!currentLineItem) {
// throw new Error("Error getting lineItem")
// }
// const lineItems = [{
// _id: currentLineItem._id,
// }]
console.log("Line Items:", lineItems)
console.log(order)
const elevatedCreateFulfillment = elevate(orderFulfillments.createFulfillment)
// Automatically fulfilling orders with stock delivery option
await wixStoresBackend.createFulfillment(order._id, { lineItems, trackingInfo })
.then((createdItem) => {
console.log("Fulfillment created", createdItem)
item.fulfilled = true // Removing this line doesn't change error
return wixStoresBackend.sendFulfillmentEmail(createdItem.order._id, orderItem.fulfillerId)
})
})
// .catch((error) => { // Hook error is normally caught by this block
// console.log("Error creating fulfillment:", error)
// notifyError("Error creating fulfillment", error, { orderNumber: order.number })
// })
console.log(`Order #${order.number} automatically tracked!`)
} else {
// Otherwise, if order is already fulfilled
console.log("Order is already: ", order.fulfillmentStatus)
// Maybe other way to handle logic, like updateFulfillment?
}
// } catch (error) {
// console.log(error.message)
// notifyError("Error generating fulfillment according to tracker", error, { trackingNumber: item.title })
// throw error
// }
}
return item
}