Skip to content

Commit

Permalink
feat(klaviyo): fetch instead of hydrate
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvdbrug committed Feb 19, 2025
1 parent a41997f commit f47a18a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/vendure-plugin-klaviyo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.6.1 (2025-02-19)

- Fetch order with relations instead of hydrating, to prevent `Maximum call stack exceeded` (https://github.com/vendure-ecommerce/vendure/issues/3355)

# 1.6.0 (2025-01-14)

- Added mutation to sign up to Klaviyo Audience list
Expand Down
2 changes: 1 addition & 1 deletion packages/vendure-plugin-klaviyo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pinelab/vendure-plugin-klaviyo",
"version": "1.6.0",
"version": "1.6.1",
"description": "An extensible plugin for sending placed orders to the Klaviyo marketing platform.",
"author": "Martijn van de Brug <[email protected]>",
"homepage": "https://pinelab-plugins.com/",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { OrderAddress } from '@vendure/common/lib/generated-types';
import {
Address,
EntityHydrator,
Logger,
OrderPlacedEvent,
OrderService,
translateDeep,
} from '@vendure/core';
import { loggerCtx } from '../constants';
Expand All @@ -12,31 +12,32 @@ import { KlaviyoOrderPlacedEventHandler } from './klaviyo-event-handler';

export const defaultOrderPlacedEventHandler: KlaviyoOrderPlacedEventHandler = {
vendureEvent: OrderPlacedEvent,
mapToKlaviyoEvent: async ({ order, ctx }, injector) => {
await injector.get(EntityHydrator).hydrate(ctx, order, {
relations: [
mapToKlaviyoEvent: async ({ order: { id, code }, ctx }, injector) => {
// Refetch order with relations. Don't hydrate to prevent concurrency issues
const order = await injector
.get(OrderService)
.findOne(ctx, id, [
'lines.productVariant.product.facetValues.facet',
'lines.productVariant.product.translations',
'lines.productVariant.collections.children',
'lines.productVariant.featuredAsset',
'shippingLines.shippingMethod',
'customer.addresses.country',
'customer.user',
],
});
order.lines.forEach((line) => {
line.productVariant.product = translateDeep(
line.productVariant.product,
ctx.languageCode
);
});
if (!order.customer) {
]);
if (!order?.customer) {
Logger.error(
`Can not send Order placed Event to Klaviyo, because order ${order.code} has no customer`,
`Can not send Order placed Event to Klaviyo, because order ${code} has no customer`,
loggerCtx
);
return false;
}
order.lines.forEach((line) => {
line.productVariant = translateDeep(
line.productVariant,
ctx.languageCode
);
});
let address: Address | OrderAddress | undefined =
order.customer.addresses.find((a) => a.defaultShippingAddress);
if (!address) {
Expand Down

0 comments on commit f47a18a

Please sign in to comment.