Last active
June 3, 2022 11:33
-
-
Save adamcbowman/ae98b5664898c4268ca975b548f1c071 to your computer and use it in GitHub Desktop.
Shopify Update Notes Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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