Built-in Modules > CanadaPost Class
CanadaPost 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 CanadaPost
class is specifically tailored to access real-time shipping rates from Canada Post.
Class Definition
class CanadaPost {
constructor(origin, destination) {
// Class constructor
}
async getRates(parcels) {
// Method to fetch rates
}
}
Arguments
origin
(Object): This represents the "origin" information from the DATA JSON object used in thecalculateShippingRates
function.destination
(Object): This refers to the "destination" information from the same DATA JSON object.
Usage Example
import { CanadaPost } from "./modules.js"
const canadaPost = new CanadaPost(DATA.origin, DATA.destination);
const parcels = [
{ length: 104, width: 30, height: 10, weight: 20, quantity: 1 },
// ... more parcels
];
const rates = await canadaPost.getRates(parcels);
In this example, the CanadaPost
class is initiated with the origin and destination data. The getRates
method is employed to determine shipping rates for a series of parcels, with each parcel specified by dimensions (length, width, height in cm) and weight (in kg). The format of the returned rates
will be in line with Shopify's shipping rates.
The CanadaPost
plug-in supports package (not envelope), returns rates in "CAD" currency. The rates are based on the information on Canada Post website. For Canada and United States destinations, a valid zip code is required; for other international destination a valid country code is required.
Sample AI Prompt
I'd like a shipping rate calculator that uses the Canada 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 { CanadaPost } 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 Canada Post
const parcels = DATA.items.map(item => {
const cartonMetafield = item.metafields && item.metafields.carton;
return {
length: cartonMetafield ? parseInt(cartonMetafield.length, 10) : 0,
width: cartonMetafield ? parseInt(cartonMetafield.width, 10) : 0,
height: cartonMetafield ? parseInt(cartonMetafield.height, 10) : 0,
weight: cartonMetafield ? parseFloat(cartonMetafield.weight) * 1000, // Convert kg to grams
quantity: item.quantity
};
}).filter(parcel => parcel.length > 0 && parcel.width > 0 && parcel.height > 0 && parcel.weight > 0);
// Initialize Canada Post
const canadaPost = new CanadaPost(DATA.origin, DATA.destination);
// Get rates from Canada Post
const rates = await canadaPost.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: "" }] };
}
}