#Sendgrid and order_placed: Send a bcc email in addition to the customer email.
16 messages · Page 1 of 1 (latest)
you may need to customize the development of the notification provider
Appreciate the help Attila but my question was more in the medusa side.
import {
type SubscriberConfig,
type SubscriberArgs,
OrderService,
} from "@medusajs/medusa";
import { relative } from "path";
export default async function handleOrderPlaced({
data,
eventName,
container,
pluginOptions,
}: SubscriberArgs<Record<string, string>>) {
const sendGridService = container.resolve("sendgridService");
const orderService: OrderService = container.resolve("orderService");
const order = await orderService.retrieve(data.id, {
relations: ["items", "shipping_address", "customer", "cart"],
});
sendGridService.sendEmail({
templateId: "xxx",
from: "xxx",
to: "xx",
dynamic_template_data: {
customer_name: `${order.customer.first_name} ${order.customer.last_name}`,
customer: order.customer.last_name,
cart: order.cart,
ordernumber: order.display_id,
total: order.total,
phone: order.customer.phone,
email: order.customer.email,
subtotal: order.subtotal,
paid_total: order.paid_total,
tax_total: order.tax_total,
shipping_total: order.shipping_total,
items: order.items.map((item) => ({
title: item.title,
quantity: item.quantity,
discounted_price: item.discount_total,
})),
shipping_address: `${order.shipping_address.address_1}, ${order.shipping_address.postal_code}`,
order_number: order.display_id,
site: "https://samistore.ma",
},
});
}
export const config: SubscriberConfig = {
event: OrderService.Events.PLACED,
context: {
subscriberId: "order-placed-handler",
},
};
I don't know why some fields are not displaying in my email, for example total property
<html>
<head>
<title>Order Confirmation</title>
</head>
<body>
<div>
<p> Cart: {{cart}} </p>
<p> Customer: {{customer}} </p>
<p> Number order: {{ordernumber}} </p>
<p> total {{total}} </p>
But it's on sendgrid side, not medusa. This is injected sendgrid function. Just add bcc.
I decided to send a brand new email with each order_placed event in addition to the confirmation email instead of adding a bcc.
This works fine except some fields are showing up in the emails (items for example) and others for a weird aren't. (such as total)
sendGridService.sendEmail({
templateId: "xxx",
from: "xxx",
to: "xx",
bcc:
Sorry for not being explicit enough.
I am using the sendGrid plugin
{
resolve: `medusa-plugin-sendgrid`,
options: {
api_key: process.env.SENDGRID_API_KEY,
from: process.env.SENDGRID_FROM,
order_placed_template: process.env.SENDGRID_ORDER_PLACED_ID,
user_password_reset_template: process.env.SENDGRID_USER_PASSWORD_RESET_ID,
},
},
I looked into the documentation, it seems sendGrid subscriber is not exposed to the codebase and does all the work behind the scenes.
That means that if I want to alter the data being sent to sendGrid, I have to modify the plugin right?
I can't create a subscriber that would update the sendGrid plugin subscriber.
So all I can do is create a new subscriber that would send a new email in addition to the email being sent by the sendgrid plugin.
I don't undestand, isn't that what you're doing in the code above?
Yes, this is what I'm doing except some properties are not displayed in my send grid email and I can't find why.
I logged my order and other entities but I can't find total
const order = await o
rderService.retrieve(data.id, {
relations: ["items", "shipping_address", "customer", "cart"],
});
console.log(order);
{
object: 'order',
id: 'order_01HRN415VBB2D0RTT3QZNWZG88',
created_at: 2024-03-10T21:24:19.683Z,
updated_at: 2024-03-10T21:24:19.683Z,
status: 'pending',
fulfillment_status: 'not_fulfilled',
payment_status: 'awaiting',
display_id: 100,
cart_id: 'cart_01HRN3ZVYBTY1RZMTHH39Z2R7D',
customer_id: 'cus_01HPKMS2BG6Q47SRRPYHHES03K',
email: 'xxx"
billing_address_id: 'addr_01HRN40G09W3DYYS81TG7ETWE7',
shipping_address_id: 'addr_01HRN3ZVYCD7BMJ39C0GECXN21',
region_id: 'reg_01HPRRRAAJ4TSTA8068N8M63ZR',
currency_code: 'mad',
tax_rate: null,
draft_order_id: null,
canceled_at: null,
metadata: {},
no_notification: null,
idempotency_key: null,
external_id: null,
sales_channel_id: 'sc_01HPKKYA5GK6G721J7T3RPHNGA',
beforeInsert: [Function (anonymous)],
beforeUpdate: [Function (anonymous)],
afterLoad: [Function (anonymous)],
items: [
LineItem {
id: 'item_01HRN3ZY40K0PR1PNYK5FEAHKS',
created_at: 2024-03-10T21:23:39.192Z,
updated_at: 2024-03-10T21:24:19.683Z,
cart_id: 'cart_01HRN3ZVYBTY1RZMTHH39Z2R7D',
order_id: 'order_01HRN415VBB2D0RTT3QZNWZG88',
swap_id: null,
claim_order_id: null,
original_item_id: null,
order_edit_id: null,
title: 'Iphone 15 - 128 GO - Vert Emeraude',
description: 'One Size',
thumbnail: 'https://medusa-public-images.s3.eu-west-1.amazonaws.com/coffee-mug.png',
is_return: false,
is_giftcard: false,
should_merge: true,
allow_discounts: true,
has_shipping: true,
unit_price: 28000,
I was expecting total to be a property of the Order entity