@aftership/shop-now

中文文档
0.0.11发布于 2026/11/05ISCTypeScriptES Module零依赖近 30 天下载 8679

Returns shop now library

原始使用文档
文档目录6 节
  1. 🚀 Pre requirement
  2. 📦 Background
  3. ✨ Features
  4. 📦 Install
  5. 🤹‍♀️ Getting Started
  6. React

@aftership/shop-now

Returns shop now library

🚀 Pre requirement

Exchange for other items SDK requires Premium or above plan. Log in to AfterShip Returns Admin to upgrade your subscription.

📦 Background

The returns efa on store flow will jump from the return page to the store with related parameters in the URL. The shop-now SDK will handle the parameters carried in the store URL and provide related capabilities, allowing you to easily complete the returns efa flow in your self-built store

✨ Features

  • 🏄🏼‍♂️ Easy to learn and use.
  • ⚛️ Supports React.
  • 🪄 Multiple custom APIs that support exchange for anything on store.
  • 🎪 Support multiple e-commerce platforms, including Shopify, Bigcommerce, and Salesforce Commerce Cloud.
  • 🎯 Written in TypeScript with predictable static types.
  • 💦 Support SSR

📦 Install

npm i @aftership/shop-now

🤹‍♀️ Getting Started

React

Here is an example to use

import {useReturnsShopNow} from '@aftership/shop-now/react';

useReturnsShopNow is a collection of logical data, it doesn't include UI. You can implement the UI yourself or use the UI banner provided by the SDK

1、First step, determine if it's in the returns efa process.You can use isFromEFA from useReturnsShopNow to determine if it's in the efa process.

import {useReturnsShopNow} from '@aftership/shop-now/react';

function YourComponent() {
  const {isFromEFA} = useReturnsShopNow({
    platform: 'shopify',
  });

  if (isFromEFA) {
    // EFA process logic here
    return <EFAFlowComponent />;
  }

  // Regular flow logic here
  return <RegularFlowComponent />;
}

2、Second step, when confirmed it's an efa process, the new page needs to call the initCart method to calculate the current shopping cart item prices.

import {useEffect} from 'react';
const {initCart, isFromEFA, shopContext} = useReturnsShopNow({
  platform: 'shopify',
});

const {isMounted, isLoading} = shopContext;

useEffect(() => {
  if (isFromEFA && isMounted) {
    initCart?.(lineItems as LineItems);
  }
}, [isFromEFA, initCart, isMounted]);

We also provide the isLoading status of the calculation API and the isMounted status of the hook. You can perform initCart after the isMounted status is true. When you have special scenarios that require using initCart, you can use isMounted. In general, using updateCart is sufficient.

shopContext contains some information you can use

export type PlatformContext = Partial<{
  shopInfo: {
    currency: string;
    // return page language
    language: string;
    // return page redirect url
    redirectUrl: string;
    // return page origin
    rc_origin: string;
    // return page navigate from path
    rc_navigate_from_path: string;
  };
  // price calculation
  price: {amount: number; currency: string};
  // returns admin page editor style
  style: {
    background: string;
    font: string;
  };
}> & {
  /**
   * fetch calculation api loading
   */
  isLoading: boolean;
  /**
   * EFA process hook loading status
   */
  isMounted: boolean;
};

Additionally, all line items should conform to the following format 👇

type LineItem = {
  external_product_id: string;
  external_variant_id: string;
  quantity: number;
};

type LineItems = LineItem[];

Once the price calculation is successful, you will get the price from shopContext.You can access the price object under shopContext to get the amount and currency. shopContext?.price;

interface Price {
  amount: number;
  currency: string;
}

3、Third step, when the shopping cart changes, you should call the updateCart method to update the cart, pass in lineItems. shopContext is a subscribed state, so when the items are calculated successfully, it will update automatically

const {updateCart} = useReturnsShopNow({
  platform: 'shopify',
});

updateCart?.(lineItems);

It is recommended to use updateCart to trigger calculations. When the cart changes, you can pass the cart data for calculation, which can effectively aggregate calculations.

In addition, I provide addToCart and removeFromCart methods based on individual item changes, allowing you to only pass in the current lineItem

const {addToCart, removeFromCart} = useReturnsShopNow({
  platform: 'shopify',
});

addToCart(lineItem);
removeFromCart(lineItem);

When you want to know the actual quantity and information of items stored inside the SDK, You can access the cart property in the shop context.

const {shopContext} = useReturnsShopNow({
  platform: 'shopify',
});

const cart = shopContext?.cart ?? [];

3.1、Setting Discount Codes

If your store has shopify discount codes applied to the cart, you can set them using the setDiscountCodes method. This will ensure the discount codes are saved along with the return intention.

const {setDiscountCodes} = useReturnsShopNow({
  platform: 'shopify',
});

// Set discount codes (e.g., from Shopify cart.js discount_codes where applicable: true)
setDiscountCodes?.(['DISCOUNT10', 'SUMMER2024']);

4、Fourth step, you can call checkout, goBack, buyNow three methods to return to the returns page."

4.1、checkout When users click the shopping cart button in the shop to check out, you should call checkout, and it will automatically redirect to the return page

const {checkout} = useReturnsShopNow({
  platform: 'shopify',
});

checkout?.();

4.2、buyNow When users click the buy now button in the shop to check out, you should call buyNow, passing in the lineItem, and it will automatically redirect to the return page

const {buyNow} = useReturnsShopNow({
  platform: 'shopify',
});

buyNow?.(lineItem);

4.3、goBack When users click the go back button in the returns page, you should call goBack, and it will automatically redirect to the shop page

const {goBack} = useReturnsShopNow({
  platform: 'shopify',
});

goBack?.();

🎉 Congratulations, you are now familiar with the core process of the efa sdk

StepFeatureKey Methods/PropertiesDescription
1Flow DetectionisFromEFA from useReturnsShopNowDetermines if current flow is EFA process
2Cart InitializationinitCart(lineItems)Initializes cart and calculates prices
3Cart OperationsupdateCart(lineItems)
addCart(lineItem)
removeFromCart(lineItem)
Methods for managing cart items
3.1Discount CodessetDiscountCodes(codes: string[])Set applicable discount codes for the cart
4Navigation Methodscheckout()
buyNow()
goBack()
Methods to return to returns page
5Price Accessprice from shopContextAccess to amount and currency information

📦 In addition, some out-of-the-box capabilities are provided

If you want to use the default Returns UI banner for display, we have already packaged it as a React component for your use.

1、ReturnsShopNowBanner

import {ReturnsShopNowBanner} from '@aftership/shop-now/react';

export interface IBannerStyleConfig {
  background?: string;
  font?: string;
  opacity?: number;
}
interface IReturnsShopNowBannerProps {
  styleConfig: IBannerStyleConfig;
  creditText: string;
}

const {shopContext} = useReturnsShopNow({
  platform: 'shopify',
});

<ReturnsShopNowBanner styleConfig={shopContext?.style} creditText={} />;

You can call shopContext?.style to get the banner styles configured in returns admin, or you can override them yourself

2、Error Handling For error handling, the SDK provides an error field to let external code be aware of errors that occur within the SDK

The SDK will return errors from the calculation API, which can be categorized into session expiration and internal API errors. If the session has expired, you need to return to the return page and initiate the return process again. If there is an internal API error, please contact us.

Note: The maximum duration for a single return EFA on store process is 30 minutes. If it exceeds 30 minutes, the session will expire.

const {error} = useReturnsShopNow({
  platform: 'shopify',
});

const isSessionExpired = error.type === 'SESSION_EXPIRED';
const isApiError = error.type === 'API_ERROR';

console.log(error);

error is a state. If you want to clear the error, you can call the clearError method to clear it.

const {clearError} = useReturnsShopNow({
  platform: 'shopify',
});

3、Use returns credit translation If you want the credit text font to be consistent with returns admin, you can call getCreditTranslationText method after calling initTranslation, passing in amount and currency to get the credit text

const {shopContext, initTranslation, getCreditTranslationText} =
  useReturnsShopNow({
    platform: 'shopify',
  });

useEffect(() => {
  initTranslation?.();
}, [initTranslation]);

const creditText = useMemo(() => {
  return (
    getCreditTranslationText?.(
      shopContext?.price?.amount,
      shopContext?.price?.currency,
    ) ?? '--'
  );
}, [shopContext]);

版本与发布记录

@aftership/shop-now 共发布 35 个版本,首次发布于 2024/05/29;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.0.11latest2026/11/051 天前
0.0.102026/03/117 个月前
0.0.92026/03/048 个月前
0.0.6-patch.12026/02/068 个月前
0.0.82025/07/281.3 年前
0.0.72025/07/281.3 年前

关于 @aftership/shop-now 的常见问题

@aftership/shop-now 是什么?

Returns shop now library

如何安装 @aftership/shop-now?

在项目目录运行 npm install @aftership/shop-now,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.2.0 || ^19.0.0)、react-dom(^18.2.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aftership/shop-now 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aftership/shop-now 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aftership/shop-now 可以商用吗?

@aftership/shop-now 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aftership/shop-now 的下载量有多少?

根据 npm 官方下载统计,@aftership/shop-now 在 2026/10/05 – 2026/11/03 这 30 天内被下载 8,679 次,日均约 289 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aftership/shop-now 还在维护吗?

最新版本 0.0.11 发布于 2026/11/05(1 天前);至今共发布 35 个版本,其中过去 12 个月发布了 4 个;首次发布于 2024/05/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。