Skip to content

Imagination-Media/magento-serverless-shipping

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magento logo      

Serverless Shipping Rates

This Magento module will extend the Magento 2 serverless framework module allowing us to use a serverless function to calculate the shipping rates for a Magento cart.


   


Installation

Requirements

  • Magento >= 2.4.5-p1
  • PHP >= 8.1

Installing the module and enabling it

To install this module we must run the composer require command requesting its installation:

composer require imdigital/magento-serverless-shipping

Then, enable it by modifying the app/etc/config.php or running the CLI command:

php bin/magento module:enable ImDigital_ServerlessShipping


Enabling delivery method

Once this module is installed, a new delivery method will be available on the Magento admin panel. It's called "Serverless Shipping Rates".

So, first, we need to add a serverless function to the serverless_functions database table, connecting Magento with the serverless function that's going to be used to calculate the shipping rates.

Then, on this delivery method, we can define a title for this delivery method and choose what's the serverless function that's going to return the shipping rates.

Delivery method configuration
Delivery method in the admin panel



Returning the shipping rates

The serverless function that returns the shipping rates must return a list of rates, and each of these rates must have the following fields:

  • code - an unique identifier for the shipping rate.
  • label - the title that's going to be displayed to the customer on the shipping calculation.
  • cost - the shipping cost for this rate.
  • price - the price for using the shipping rate. It's the value the customer is going to pay when choosing the shipping rate.

Example (in NodeJS):

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.calculateShippingRates = (req, res) => {
  const shippingRates = [
    {
      'code' : 'rate1',
      'label' : 'Rate 1',
      'cost' : 10.50,
      'price' : 17
    },
    {
      'code' : 'rate2',
      'label' : 'Rate 2',
      'cost' : 18,
      'price' : 25
    }
  ];

  res.status(200).send(JSON.stringify(shippingRates));
};

About

Get shipping rates using a serverless function

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages