Main site

Built-in Modules > Bellewholesale Class

Bellewholesale 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 Bellewholesale class is designed to retrieve real-time shipping rates for products available on Bellewholesale.

Class Definition

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

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

Arguments

  • destination (Object): The "destination" field from the input DATA JSON object used by the calculateShippingRates function.
  • currency (String): The currency code in ISO format for which you need the shipping rates.

Usage Example

import { Bellewholesale } from "./modules.js"
const bellewholesale = new Bellewholesale(DATA.destination, DATA.currency);
const bellewholesaleProducts = [
  { bellewholesale_variant_id: "1366613z9r8C8d5t3R3W402w35", quantity: 1 },
  { bellewholesale_variant_id: "1M6Z7l3v4K0l4C6y6K2U8u4K0G", quantity: 1 },
  // ... additional products
];
const rates = await bellewholesale.getRates(bellewholesaleProducts);

In the given example, the Bellewholesale class is instantiated with destination details and currency code. The getRates method then fetches shipping rates for a list of Bellewholesale product variants, each identified by a bellewholesale_variant_id and quantity. The structure of the returned rates is consistent with the format of Shopify's shipping rates.

Ask Bellewholesale to include the items variant id in their data feed to get bellewholesale_variant_id.

Sample AI Prompt

I'd like a shipping rate calculator for items from Bellewholesale.
Enrich the data with metafield namespace "shop" with size 10.
The variant id for each Bellewholesale items is saved in the data items metafield object "shop" namespace with key named "bellewholesale_variant_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 { Bellewholesale } 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 Bellewholesale products
    const bellewholesaleProducts = DATA.items.map((item) => {
      const bellewholesaleVariantId = item.metafields?.shop?.find(
        (metafield) => metafield.key === "bellewholesale_variant_id"
      )?.value;
      return { bellewholesale_variant_id: bellewholesaleVariantId, quantity: item.quantity };
    });

    // Get shipping rates from Bellewholesale
    const bellewholesale = new Bellewholesale(DATA.destination, DATA.currency);
    const rates = await bellewholesale.getRates(bellewholesaleProducts);

    // Return only the option with the minimum total price
    const minPriceRate = rates.reduce((acc, rate) => {
      if (!acc || rate.total_price < acc.total_price) {
        return rate;
      }
      return acc;
    }, null);

    return { rates: [minPriceRate] };
  } catch (error) {
    // Return a rate with price equals to null, and other rate fields set to empty string
    return {
      rates: [
        {
          service_name: "",
          service_code: "",
          total_price: null,
          description: "",
          currency: "",
          min_delivery_date: "",
          max_delivery_date: "",
        },
      ],
    };
  }
}