The reality of support for Magento 2 shipping methods in headless storefronts

Marcin Kwiatkowski /
The reality of support for Magento 2 shipping methods in headless storefronts

Magento 2 as an eCommerce platform offers good support for various shipping methods so typically you are able to find integration for the shipping methods you want. Until you want to build a headless storefront connected with Magento 2, the sentence above for sure, but how support for Magento 2 shipping methods looks when you want to:

  • - build a PWA storefront using PWA Studio?

  • - build a headless storefront using Vue Storefront?

  • - build your own custom headless storefront?

I did a small investigation, and in this article, I would like to show you what is the support for shipping methods, and what options you have when you want to create your own custom shipping method.

Three default Magento 2 shipping methods

Magento 2 offers three simple shipping options:

  • - flat rate - straightforward shipping method with fixed prices for each item.

  • - table rates - the shipping rates in tables calculate shipping costs using conditions like Weight v. Destination, Price v. Destination, and Number of Items v. Destination. Data used for calculation came from a predefined spreadsheet that is imported to a store

  • - free shipping - you can set free shipping based on minimum amount of an order, or set up a cart price rule that is applied when a set of conditions is me

How to configure Magento 2 shipping settings

To configure shipping methods you need to open an admin panel and go to Stores > Configuration > Sales > Delivery methods. You should see the shipping methods configuration:

magento 2 shipping methods

Each shipping method has a similar set of configuration fields. Take a look at the flat rate shipping method:

Magento 2 flat rate shipping method

Shipping methods configuration fields

  • - enabled - determinates is shipping method is enabled for a specific scope

  • - title - the name of the shipping method that is used on the Magento 2 checkout page

  • - method name - the name of the method used next to the title on the checkout page. For example, the default phrase visible on the checkout page for the Flat rate shipping method is "Flat rate - fixed"

  • - price - the price of the shipping method that you charge a customer

  • calculate handling fee - the way the handling fee is calculated if included. Options: Fixed / Percent

  • - handling fee - the amount to be charged for a handling fee, based on the method you have chosen to calculate the amount (fixed or percent)

  • - displayed error message - a message appears in the case when the method is not available

  • - ship to applicable countries - thanks to this option you can specify for which countries shipping method is available

  • - ship to specific countries - similar to the previous one, but here you can explicitly set countries

  • - show method if not applicable - if this option is set to yes, a shipping method will be visible on checkout even is not applicable for some reason.

  • - sort order - a number determines the position of this method in the delivery methods list visible on the checkout page

Configure shipping methods in Magento

Please follow these tutorials to configure default shipping methods in Magento:

When you complete and go to the Magento checkout page you should see something like this:

shipping costs in Magento 2 checkout for free shipping method, table rate and flat rate (defaults)

Magento 2 shipping methods in headless storefronts

In case when you wanted to build a headless storefront for Magento, you would have considered three options:

  • - use PWA Studio

  • - use Vue Storefront

  • - build custom frontend

In either case, the Magento 2 GraphQL API is a source of data, so let's take a look hwo to retreive information about shipping methods from GraphQL and which data is available.

Fetch shipping methods from Magento GraphQL API

Create empty Cart

Firstly, we have to create an empty cart. To do so, send this mutation to the API:

1mutation {
2  createEmptyCart(input: {})
3}

The mutation returns a new cart id like this:

1{
2  "data": {
3    "createEmptyCart": "K9tlVNTGnyKk9b7Lw8xLnByORqQUhRGR"
4  }
5}

Add product to cart

Once we have a new cart, we can add products to it. Before we do that, we fetch information about products from Magento.

Fetch products information
1query {
2  products(search: "bag", pageSize: 5) {
3    items {
4      uid
5      name
6    }
7  }
8}

The query above returns five products that pass to the "bag" search term. The SKU field is important to us because we will use it in the next step. Here we have our five bags:

1{
2  "data": {
3    "products": {
4      "items": [
5        {
6          "sku": "24-MB01",
7          "name": "Joust Duffle Bag"
8        },
9        {
10          "sku": "24-WB01",
11          "name": "Voyage Yoga Bag"
12        },
13        {
14          "sku": "24-MB05",
15          "name": "Wayfarer Messenger Bag"
16        },
17        {
18          "sku": "24-WB04",
19          "name": "Push It Messenger Bag"
20        },
21        {
22          "sku": "24-UG03",
23          "name": "Harmony Lumaflex™ Strength Band Kit "
24        }
25      ]
26    }
27  }
28}
Add product to cart using addProductsToCartMutation

Once we know the cart ID, and some products IDs, we can add products to the cart:

1mutation {
2  addProductsToCart(cartId: "K9tlVNTGnyKk9b7Lw8xLnByORqQUhRGR", cartItems: [{ sku: "24-MB01", quantity: 1 }]) {
3    cart {
4      total_quantity
5      items {
6        product {
7          sku
8        }
9      }
10    }
11
12  }
13}
14

In the query above I added a product with SKU 24-Mb01 to the cart. Results:

1{
2  "data": {
3    "addProductsToCart": {
4      "cart": {
5        "total_quantity": 1,
6        "items": [
7          {
8            "product": {
9              "sku": "24-MB01"
10            }
11          }
12        ]
13      }
14    }
15  }
16}

Set shipping address on the cart

The shipping address is the last missing thing that we have to add to see available shipping methods. To add the shipping address we need to use the setShippingAddressesOnCart mutation:

1mutation {
2  setShippingAddressesOnCart(
3    input: {
4      cart_id: "K9tlVNTGnyKk9b7Lw8xLnByORqQUhRGR"
5      shipping_addresses: [
6        {
7          address: {
8            firstname: "Bob"
9            lastname: "Roll"
10            company: "Magento"
11            street: ["Magento Pkwy", "Main Street"]
12            city: "Austin"
13            region: "TX"
14            postcode: "78758"
15            country_code: "US"
16            telephone: "8675309"
17            save_in_address_book: false
18          }
19
20        }
21      ]
22    }
23  ) {
24    cart {
25      shipping_addresses {
26        firstname
27        lastname
28        company
29        street
30        city
31        region {
32          code
33          label
34        }
35        postcode
36        telephone
37        country {
38          code
39          label
40        }
41      }
42    }
43  }
44}

When I send this mutation to the API, Magento will set the shipping address on the cart. Results:

1{
2  "data": {
3    "setShippingAddressesOnCart": {
4      "cart": {
5        "shipping_addresses": [
6          {
7            "firstname": "Bob",
8            "lastname": "Roll",
9            "company": "Magento",
10            "street": [
11              "Magento Pkwy",
12              "Main Street"
13            ],
14            "city": "Austin",
15            "region": {
16              "code": "TX",
17              "label": "Texas"
18            },
19            "postcode": "78758",
20            "telephone": "8675309",
21            "country": {
22              "code": "US",
23              "label": "US"
24            }
25          }
26        ]
27      }
28    }
29  }
30}

Fetch available shipping methods from Magento 2 GraphQL API

We have everything to get information about shipping methods, so we can use this query:

1query {
2    cart(cart_id: "K9tlVNTGnyKk9b7Lw8xLnByORqQUhRGR") {
3    	shipping_addresses {
4        available_shipping_methods {
5          amount {
6            currency
7            value
8          }
9          available
10          carrier_code
11          carrier_title
12          error_message
13          method_code
14          method_title
15          price_excl_tax {
16            currency
17            value
18          }
19          price_incl_tax {
20            currency
21            value
22          }
23        }
24      }
25    }
26}

Magento returns available shipping methods, and as you can see below, all three mentioned earlier shipping methods (flat rate, table rate, and free shipping) are available:

1{
2  "data": {
3    "cart": {
4      "shipping_addresses": [
5        {
6          "available_shipping_methods": [
7            {
8              "amount": {
9                "currency": "USD",
10                "value": 0
11              },
12              "available": true,
13              "carrier_code": "freeshipping",
14              "carrier_title": "Free Shipping",
15              "error_message": "",
16              "method_code": "freeshipping",
17              "method_title": "Free",
18              "price_excl_tax": {
19                "currency": "USD",
20                "value": 0
21              },
22              "price_incl_tax": {
23                "currency": "USD",
24                "value": 0
25              }
26            },
27            {
28              "amount": {
29                "currency": "USD",
30                "value": 10
31              },
32              "available": true,
33              "carrier_code": "flatrate",
34              "carrier_title": "Flat Rate",
35              "error_message": "",
36              "method_code": "flatrate",
37              "method_title": "Fixed",
38              "price_excl_tax": {
39                "currency": "USD",
40                "value": 10
41              },
42              "price_incl_tax": {
43                "currency": "USD",
44                "value": 10
45              }
46            },
47            {
48              "amount": {
49                "currency": "USD",
50                "value": 15
51              },
52              "available": true,
53              "carrier_code": "tablerate",
54              "carrier_title": "Best Way",
55              "error_message": "",
56              "method_code": "bestway",
57              "method_title": "Table Rate",
58              "price_excl_tax": {
59                "currency": "USD",
60                "value": 15
61              },
62              "price_incl_tax": {
63                "currency": "USD",
64                "value": 15
65              }
66            }
67          ]
68        }
69      ]
70    }
71  }
72}

Support for shipping methods in GraphQL API and headless storefronts

Thanks to Magento 2 GraphQL API you can fetch information about basic delivery methods and use them on your headless storefront. Summarizing - these shipping methods:

  • - flat rate

  • - table rate

  • - free shipping

are available in Vue Storefront, and PWA Studio, and they are easy to implement in custom storefronts.

How about other shipping methods?

Magento has a few other shipping methods:

  1. In-Store Delivery

  2. UPS

  3. USPS

  4. FedEx

  5. DHL

In-Store Delivery

The in-store delivery method allows choosing a pickup point when a customer wants to pick up an order. This feature is not supported in PWA STUDIO and Vue Storefront when I write this article.

Magento 2 GraphQL allows to set pickup point as a shipping address, and it's possible to fetch pickup points by using the pickupLocations query so I assume that it's possible to create custom code that covers this functionality in custom headless storefronts.

UPS, USPS, FedEx, DHL

No support in PWA Studio and Vue Storefront. Custom development is needed.

Custom shipping methods

What about custom shipping methods? You may ask: Is GraphQL return information about custom delivery methods among the default ones?

Just for test, I created a new delivery method called “Custom Delivery.”

You can find the Magento 2 module containing this delivery method on my Github.

It's returned from API, and available in PWA Studio, and Vue Storefront as well.

Summary

Shipping methods are available for PWA Studio, Vue Storefront, and custom headless storefronts through GraphQL queries, and they are displayed on the storefront. Custom shipping methods also are shown as well. In-Store pickup and other methods than free shipping, flat rate, and table rate are not supported and custom development is needed. Anyway, for many clients, the default shipping methods are good enough and they can consider a headless storefront as a possible solution. I expect that in the future, the amount of supported shipping methods will be bigger, and maybe some extensions from external providers will be available.

Share this article with your friends!

Each share supports and motivates me to create new content. Thank you!

About the author

Marcin Kwiatkowski

Frontend developer, Certified Magento Full Stack Developer, writer, based in Poland. He has eight years of professional Software engineering experience. Privately, husband and father. He likes reading books, drawing, and walking through mountains.

Read more