Main site

Built-in Modules > Banggood Class

Banggood 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 Banggood class allows for fetching real-time shipping rates for products from Banggood.

Class Definition

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

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

Arguments

  • destination (Object): The "destination" part of the DATA JSON object that's passed to the calculateShippingRates function.
  • currency (String): The currency code in ISO format that you want the shipping rates to be calculated in.

Usage Example

import { Banggood } from "./modules.js"
const banggood = new Banggood(DATA.destination, DATA.currency);
const banggoodProducts = [
  { banggood_product_id: "1891731", quantity: 1 },
  { banggood_product_id: "1694713", quantity: 2 },
  // ... more products
];
const rates = await banggood.getRates(banggoodProducts);

This example demonstrates how to instantiate the Banggood class with the destination information and currency. getRates is then used to fetch the shipping rates for an array of products, where each product is specified by banggood_product_id and quantity. The returned rates will adhere to the same format as Shopify's shipping rates.

The banggood_product_id is the last string of the Banggood product page url. For example, https://www.banggood.com/MJX-16208-16209-HYPER-GO-1-or-16-Brushless-High-Speed-RC-Car-Vechile-Models-45km-or-h-p-1967165.html, the product id is 1967165. The id is also displayed on the product page, for example, as ID: 1967165.

Sample AI Prompt

I'd like a shipping rate calculator for items from Banggood products.
The Banggood product id is contained part of the items sku separted with a hyphen. Split the sku and take the first part as Banggood product id.

Sample AI Output

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

export async function calculateShippingRates(DATA, env) {
  try {
    // Enrich items with additional details
    DATA = await enrichItemDetails(DATA);

    // Extract Banggood product ids from SKUs
    const banggoodProducts = DATA.items.map((item) => {
      const banggoodProductId = item.sku.split("-")[0];
      return { banggood_product_id: banggoodProductId, quantity: item.quantity };
    });

    // Initialize Banggood instance with destination and currency
    const banggood = new Banggood(DATA.destination, DATA.currency);

    // Get shipping rates from Banggood
    const rates = await banggood.getRates(banggoodProducts);

    // Return shipping rates in Shopify format
    return { rates };
  } catch (error) {
    // Return error rate
    return {
      rates: [
        {
          service_name: "",
          service_code: "",
          total_price: null,
          description: "",
          currency: "",
          min_delivery_date: "",
          max_delivery_date: "",
        },
      ],
    };
  }
}