Merge branch 'main' of https://git.metatroncube.in/MetatroncubeSoftwareSolutions/shopify-custom-email-plugin
This commit is contained in:
commit
6435a014c3
@ -6,7 +6,7 @@ This is a custom Node.js Express server that listens for Shopify `orders/create`
|
|||||||
|
|
||||||
- Node.js (v14 or higher)
|
- Node.js (v14 or higher)
|
||||||
- A Shopify Store
|
- A Shopify Store
|
||||||
- An Email Account (e.g., Gmail, SendGrid) to send emails via SMTP.
|
- An Email Account (e.g ., Gmail, SendGrid) to send emails via SMTP.
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
|
|||||||
11
mailer.js
11
mailer.js
@ -74,6 +74,9 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer, shippingLa
|
|||||||
|
|
||||||
// --- 3. Billing Address Details HTML ---
|
// --- 3. Billing Address Details HTML ---
|
||||||
const billing = orderData.billing_address || {};
|
const billing = orderData.billing_address || {};
|
||||||
|
const whatsappNumber = orderData.note_attributes?.find?.((attr) => attr.name === 'WhatsApp Number')?.value;
|
||||||
|
const contactNumber = billing.phone || orderData.phone;
|
||||||
|
|
||||||
let billingAddressHtml = '';
|
let billingAddressHtml = '';
|
||||||
if (billing.first_name || billing.last_name) {
|
if (billing.first_name || billing.last_name) {
|
||||||
billingAddressHtml += `${billing.first_name || ''} ${billing.last_name || ''}<br/>`;
|
billingAddressHtml += `${billing.first_name || ''} ${billing.last_name || ''}<br/>`;
|
||||||
@ -84,8 +87,12 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer, shippingLa
|
|||||||
billingAddressHtml += `${billing.city || ''} ${billing.province || ''} ${billing.zip || ''}<br/>`;
|
billingAddressHtml += `${billing.city || ''} ${billing.province || ''} ${billing.zip || ''}<br/>`;
|
||||||
}
|
}
|
||||||
if (billing.country) billingAddressHtml += `${billing.country}<br/>`;
|
if (billing.country) billingAddressHtml += `${billing.country}<br/>`;
|
||||||
if (billing.phone || orderData.phone) {
|
if (whatsappNumber) {
|
||||||
billingAddressHtml += `${billing.phone || orderData.phone}`;
|
billingAddressHtml += `WhatsApp: ${whatsappNumber}`;
|
||||||
|
}
|
||||||
|
if (contactNumber) {
|
||||||
|
if (whatsappNumber) billingAddressHtml += `<br/>`;
|
||||||
|
billingAddressHtml += `Contact Number: ${contactNumber}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const paymentMethod = orderData.gateway || 'Request Quote';
|
const paymentMethod = orderData.gateway || 'Request Quote';
|
||||||
|
|||||||
@ -132,6 +132,8 @@ const generateInvoicePDF = async (orderData) => {
|
|||||||
|
|
||||||
// Bill To (Left)
|
// Bill To (Left)
|
||||||
const billing = orderData.billing_address || {};
|
const billing = orderData.billing_address || {};
|
||||||
|
const whatsappNumber = orderData.note_attributes?.find?.((attr) => attr.name === 'WhatsApp Number')?.value;
|
||||||
|
|
||||||
doc.fontSize(11)
|
doc.fontSize(11)
|
||||||
.font('Helvetica-Bold')
|
.font('Helvetica-Bold')
|
||||||
.fillColor(primaryColor)
|
.fillColor(primaryColor)
|
||||||
@ -166,8 +168,13 @@ const generateInvoicePDF = async (orderData) => {
|
|||||||
doc.text(orderData.email || orderData.contact_email, 40, billTextY);
|
doc.text(orderData.email || orderData.contact_email, 40, billTextY);
|
||||||
billTextY += 13;
|
billTextY += 13;
|
||||||
}
|
}
|
||||||
|
if (whatsappNumber) {
|
||||||
|
doc.text(`WhatsApp: ${whatsappNumber}`, 40, billTextY);
|
||||||
|
billTextY += 13;
|
||||||
|
}
|
||||||
if (billing.phone || orderData.phone) {
|
if (billing.phone || orderData.phone) {
|
||||||
doc.text(billing.phone || orderData.phone, 40, billTextY);
|
doc.text(`Contact Number: ${billing.phone || orderData.phone}`, 40, billTextY);
|
||||||
|
billTextY += 13;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Order Details (Right)
|
// Order Details (Right)
|
||||||
|
|||||||
16
server.js
16
server.js
@ -7,7 +7,11 @@ const { generateShippingLabelPDF } = require('./shippingLabelGenerator');
|
|||||||
const { sendEmailWithAttachment } = require('./mailer');
|
const { sendEmailWithAttachment } = require('./mailer');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(cors()); // Allow frontend Shopify store to call this server
|
app.use(cors({
|
||||||
|
origin: '*', // Allow all origins (you can restrict this to 'https://rayaarishop.com' in production)
|
||||||
|
methods: ['GET', 'POST', 'OPTIONS', 'PUT', 'PATCH', 'DELETE'],
|
||||||
|
allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'X-Shopify-Hmac-Sha256', 'X-Shopify-Access-Token']
|
||||||
|
})); // Allow frontend Shopify store to call this server
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
||||||
// Middleware to capture raw body for Shopify webhook HMAC verification
|
// Middleware to capture raw body for Shopify webhook HMAC verification
|
||||||
@ -208,6 +212,16 @@ app.post('/create-order', async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const whatsappNumber = customerData.whatsapp_number || customerData.contact_phone || customerData.contactNumber;
|
||||||
|
if (whatsappNumber) {
|
||||||
|
orderPayload.order.note_attributes = [
|
||||||
|
{
|
||||||
|
name: 'WhatsApp Number',
|
||||||
|
value: whatsappNumber
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
const shopifyResponse = await fetch(`https://${process.env.SHOPIFY_STORE_DOMAIN}/admin/api/2024-01/orders.json`, {
|
const shopifyResponse = await fetch(`https://${process.env.SHOPIFY_STORE_DOMAIN}/admin/api/2024-01/orders.json`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user