Main site

Built-in Modules > DHgate Class

DHgate Class

Important: This plugin relies on third-party resources. The resources might change or be discontinued without notice. Ensure you test the plugin extensively before production use, implement error handling with a try-catch block, have a backup shipping rate ready, and reach out to our support with detailed descriptions of any unexpected issues.

Overview

The DHgate class provides a method to retrieve real-time shipping rates for products listed on DHgate.

Class Definition

class DHgate {
  constructor(destination, currency) {
    // Class constructor
  }

  async getRates(dhgateProducts) {
    // Method to fetch rates
  }
}

Arguments

  • destination (Object): Corresponds to the "destination" field in the input DATA JSON object provided to calculateShippingRates.
  • currency (String): The currency ISO code for which you want to retrieve shipping rates.

Usage Example

import { DHgate } from "./modules.js"
const dhgate = new DHgate(DATA.destination, DATA.currency);
const dhgateProducts = [
  { dhgate_product_id: "588501194", quantity: 1 },
  { dhgate_product_id: "588501194", quantity: 2 },
  // ... more products
];
const rates = await dhgate.getRates(dhgateProducts);

In this example, dhgateProducts is an array of objects with dhgate_product_id and quantity keys. The getRates method is used to fetch the shipping rates, which will be formatted similarly to Shopify's shipping rates.

The dhgate_product_id is the last pathname of the DHgate product page url. For example, https://www.dhgate.com/product/hellstar-shirt-designer-t-shirts-t-shirt/906962612.html, the product id is 906962612.

The output rates has similar format as the Shopify rates array:

rates = [
    {
        "service_name": "String",
        "service_code": "String",
        "total_price": "String",
        "description": "String",
        "currency": "String",
        "min_delivery_date": "String (Date format)",
        "max_delivery_date": "String (Date format)"
    },
    //...might have more rates option
]

Sample AI Prompt

I'd like a shipping rate calculator for items from DHgate.
Enrich the data with metafield namespace "shop" with size 10.
The product id for each dhgate items is saved in the data items metafield object "shop" namespace with key named "dhgate_id". 
In case of multiple rates option, return only the option with the minimum total price.

Sample AI Output

import { enrichItemDetails } from "./modules.js";
import { DHgate } from "./modules.js";

export async function calculateShippingRates(DATA, env) {
  try {
    // Enrich data with metafield namespace "shop" with size 10
    DATA = await enrichItemDetails(DATA, [{ namespace: "shop", size: 10 }]);

    // Get DHgate products from metafield namespace "shop" with key "dhgate_id"
    const dhgateProducts = DATA.items.map((item) => {
      const dhgateId = item.metafields.shop.find(
        (metafield) => metafield.key === "dhgate_id"
      )?.value;
      return { dhgate_product_id: dhgateId, quantity: item.quantity };
    });

    // Fetch real-time shipping rates for DHgate products
    const dhgate = new DHgate(DATA.destination, DATA.currency);
    const rates = await dhgate.getRates(dhgateProducts);

    // Return only the option with the minimum total price
    return {
      rates: rates.length > 0 ? [rates.reduce((a, b) => (a.total_price < b.total_price ? a : b))] : [],
    };
  } catch (error) {
    // Return error rate
    return {
      rates: [
        {
          service_name: "",
          service_code: "",
          total_price: null,
          description: "",
          currency: "",
          min_delivery_date: "",
          max_delivery_date: "",
        },
      ],
    };
  }
}