Skip to content

Commit

Permalink
human price
Browse files Browse the repository at this point in the history
  • Loading branch information
lulu committed May 9, 2024
1 parent 94bf1fd commit 376f5cf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,16 @@ I strongly recommend using this plugin in combination with the [Medusa Plugin Se
### 4\. The Sendgrid Template receives the following
```ts
interface TransformedCart {
export type NewLineItem = Omit<LineItem, "beforeUpdate" | "afterUpdateOrLoad"> & {
// human readable price with currency code
price: string
}
// The cart object is transformed to this format before sending the email
export interface TransformedCart {
id: string;
email: string;
items: LineItem[];
items: NewLineItem[];
cart_context: Record<string, unknown>;
first_name: string;
last_name: string;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medusa-plugin-abandoned-cart",
"version": "2.0.1",
"version": "2.0.2",
"description": "Medusa plugin for abandoned cart tracking and recovery",
"author": "Lucjan Grzesik (https://github.com/luluhoc)",
"license": "MIT",
Expand Down
25 changes: 23 additions & 2 deletions src/services/abandoned-cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
} from "@medusajs/medusa";
import { Lifetime } from "awilix";
import CartRepository from "@medusajs/medusa/dist/repositories/cart";
import { MedusaError } from "medusa-core-utils";
import { MedusaError, humanizeAmount, zeroDecimalCurrencies } from "medusa-core-utils";
import type SendGridService from "medusa-plugin-sendgrid-typescript/dist/services/sendgrid";

import {
AutomatedAbandonedCart,
IntervalOptions,
Expand Down Expand Up @@ -201,6 +202,17 @@ export default class AbandonedCartService extends TransactionBaseService {
return (options as AutomatedAbandonedCart).intervals !== undefined;
};

humanPrice_(amount: number | null | undefined, currency: string) {
if (!amount) {
return "0.00"
}

const normalized = humanizeAmount(amount, currency)
return normalized.toFixed(
zeroDecimalCurrencies.includes(currency.toLowerCase()) ? 0 : 2
)
}

queryBuilder = (
type: "getCount" | "getMany",
take: number,
Expand Down Expand Up @@ -313,14 +325,23 @@ export default class AbandonedCartService extends TransactionBaseService {
return {
id: cart.id,
email: cart.email,
items: cart.items,
items: cart.items.map((item) => {
return {
...item,
price: `${this.humanPrice_(
item.unit_price,
cart.region.currency_code
)} ${cart.region.currency_code}`,
}
}),
cart_context: cart.context,
first_name: cart.shipping_address?.first_name,
last_name: cart.shipping_address?.last_name,
totalPrice: cart.items.reduce(
(acc, item) => acc + item.unit_price * item.quantity,
0,
),

created_at: cart.created_at,
currency: cart.region.currency_code,
region: cart.region.id,
Expand Down
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ export interface ManualAbandonedCart extends BasePluginOptions {
export type PluginOptions = AutomatedAbandonedCart | ManualAbandonedCart


export type NewLineItem = Omit<LineItem, "beforeUpdate" | "afterUpdateOrLoad"> & {
price: string
}

export interface TransformedCart {
id: string;
email: string;
items: LineItem[];
items: NewLineItem[];
cart_context: Record<string, unknown>;
first_name: string;
last_name: string;
Expand Down

0 comments on commit 376f5cf

Please sign in to comment.