Skip to content

Instantly share code, notes, and snippets.

@adamcbowman
Last active June 3, 2022 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcbowman/ae98b5664898c4268ca975b548f1c071 to your computer and use it in GitHub Desktop.
Save adamcbowman/ae98b5664898c4268ca975b548f1c071 to your computer and use it in GitHub Desktop.
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