Main site

Built-in Modules > AustPost Class

AustPost 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 AustPost class is designed to fetch real-time shipping rates from Australia Post.

Class Definition

class AustPost {
  constructor(origin, destination) {
    // Class constructor
  }

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

Arguments

  • origin (Object): This corresponds to the "origin" field in the DATA JSON object used in the calculateShippingRates function.
  • destination (Object): This represents the "destination" field in the same DATA JSON object.

Usage Example

import { AustPost } from "./modules.js"
const austPost = new AustPost(DATA.origin, DATA.destination);
const parcels = [
  { length: 104, width: 30, height: 10, weight: 20, quantity: 1 },
  // ... more parcels
];
const rates = await austPost.getRates(parcels);

In this example, the AustPost class is initiated with the origin and destination information. The getRates method is used to determine shipping rates for an array of parcels, each defined by dimensions (length, width, height in cm) and weight (in kg). The returned rates are presented in the same format as those used by Shopify.

The AustPost plug-in supports parcels and the "Use your own packing" service option provided by Australia Post. For international shipping, please confirm the country is supported by Australia Post; the rates would generate null shipping price if the country is not supported. The rates are based on the information on Australia Post website.

Sample AI Prompt

I'd like a shipping rate calculator that uses the Australia Post rates.
The dimensions of the parcels are contained in the items metafield namespace object called "carton". Get the first 15 values in the namespace.

Sample AI Output

import { enrichItemDetails } from './modules.js';
import { AustPost } from './modules.js';

export async function calculateShippingRates(DATA, env) {
    try {
        // Enrich item details with 'carton' metafield
        DATA = await enrichItemDetails(DATA, [{ namespace: "carton", size: 15 }]);

        // Prepare parcels array for AustPost
        const parcels = DATA.items.map(item => {
            const cartonMetafield = item.metafields && item.metafields.carton;
            return {
                length: cartonMetafield ? parseInt(cartonMetafield.length) : 0,
                width: cartonMetafield ? parseInt(cartonMetafield.width) : 0,
                height: cartonMetafield ? parseInt(cartonMetafield.height) : 0,
                weight: item.grams / 1000, // Convert grams to kg
                quantity: item.quantity
            };
        }).filter(parcel => parcel.length > 0 && parcel.width > 0 && parcel.height > 0);

        // Get rates from AustPost
        const austPost = new AustPost(DATA.origin, DATA.destination);
        const rates = await austPost.getRates(parcels);

        return { rates };
    } catch (error) {
        // Return a rate with price equals to null in case of errors
        return { rates: [{ service_name: "", service_code: "", total_price: null, description: "", currency: "", min_delivery_date: "", max_delivery_date: "" }] };
    }
}