Main site

Built-in Modules > Dearlover Class

Dearlover 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 Dearlover class enables the retrieval of real-time shipping rates for Dearlover products.

Class Definition

class Dearlover {
  constructor(destination) {
    // Class constructor
  }

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

Arguments

  • destination (Object): The "destination" information taken from the DATA JSON object provided to the calculateShippingRates function.

Usage Example

import { Dearlover } from "./modules.js"
const dearlover = new Dearlover(DATA.destination);
const dearloverProducts = [
  { dearlover_product_weight: 0.4, quantity: 1 },
  { dearlover_product_weight: 1, quantity: 1 },
  // ... more products
];
const rates = await dearlover.getRates(dearloverProducts);

This usage example shows how to instantiate the Dearlover class with the destination parameter. The getRates method computes shipping rates for an array of Dearlover products, identified by their weight (dearlover_product_weight in kilograms) and the quantity of each product. The format of the returned rates will be consistent with Shopify's shipping rates.

The Dearlover plug-in returns rates in USD currency. Use our built-in currency converter class Currency to convert to other currencies. The rates are calculated based on the information on Dearlover website

Sample AI Prompt

I'd like a shipping rate calculator for items from Dearlover products.
Use the items weight as Dearlover product weight.

Sample AI Output

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

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

    // Get Dearlover products from items
    const dearloverProducts = DATA.items.map((item) => {
      return {
        dearlover_product_weight: item.grams / 1000,
        quantity: item.quantity,
      };
    });

    // Create a Dearlover instance with destination
    const dearlover = new Dearlover(DATA.destination);

    // Get rates from Dearlover
    const rates = await dearlover.getRates(dearloverProducts);

    // Return 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: "",
        },
      ],
    };
  }
}