Skip to content

Instantly share code, notes, and snippets.

@adamcbowman
Last active Jun 3, 2022
Embed
What would you like to do?
Shopify Update Notes Script
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const app = express();
const port = process.env.PORT || 8081;
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.listen(port, () => console.log(`App listening on port: ${port}`));
app.get('/', (req, res) => {
res.send('we be jammin');
});
app.post('/newOrderHook', (req, res) => {
res.status(200).send('OK');
const orderData = req.body;
getOrderNoteData(orderData);
});
function getOrderNoteData(orderData) {
const id = orderData.id;
const note = orderData.note;
const noteAttribs = orderData.note_attributes;
console.log('new order received: ' + id);
updateOrderNote(id, note, noteAttribs);
}
function updateOrderNote(id, note, noteAttribs) {
if (note) {
note = note + ', \n';
}
if (noteAttribs) {
noteAttribs.forEach((attrib) => {
const { name, value } = attrib;
if (
name === 'Pickup-Date' ||
name === 'Delivery-Date' ||
name === 'Handwritten Note' ||
name === 'Handwritten Note Name'
) {
return (note += `${name}: ${value}, \n`);
}
});
}
sendUpdatedOrderNotes(id, note);
}
function sendUpdatedOrderNotes(id, notes) {
const options = {
url: `##APIurl@shop.myshopify.com/admin/api/2021-10/orders/${id}.json##`,
json: true,
body: {
order: {
id: id,
note: notes,
},
},
};
request.put(options, (err, res, body) => {
if (err) {
return console.log(err);
}
console.log(`Status: ${res.statusCode}`);
console.log('Notes updated');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment