Shipping use cases

This page explores different use cases and scenarios for implementing shipping methods using the Adobe Commerce checkout starter kit.

For more general use cases, refer to use-cases.

Shipping methods

You can add shipping methods to the checkout process by using webhooks.

To add shipping methods, you must run a script to automatically create shipping carriers or create shipping carriers manually using the REST API. Only shipping methods with registered carriers are available in the checkout process.

After the webhook is registered, every time a shopping cart is requested, a synchronous call is dispatched to the App Builder application implementing the shipping method to calculate the shipping cost and provide the available shipping methods.

Refer to actions/shipping-methods.js for an example of how to process the request and return the list of available shipping methods.

PaaS Only To register a webhook, you need to create a webhooks.xml configuration file in your module or in the root app/etc directory.

The following example demonstrates how to add a webhook to the plugin.magento.out_of_process_shipping_methods.api.shipping_rate_repository.get_rates method:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_AdobeCommerceWebhooks:etc/webhooks.xsd">
    <method name="plugin.magento.out_of_process_shipping_methods.api.shipping_rate_repository.get_rates" type="after">
        <hooks>
            <batch name="dps">
                <hook name="add_shipping_rates_dps" url="https://<your_app_builder>.runtime.adobe.io/api/v1/web/shipping-method/shipping-methods" method="POST" timeout="5000" softTimeout="1000" priority="100" required="true">
                    <fields>
                        <field name="rateRequest" />
                    </fields>
                </hook>
            </batch>
        </hooks>
    </method>
</config>

You can register multiple webhooks for different shipping methods or shipping carriers by adding them into the same batch to ensure they are executed in parallel or create multiple batches to execute them sequentially.

SaaS Only SaaS webhooks have slightly different naming conventions. For this example, use the plugin.out_of_process_shipping_methods.api.shipping_rate_repository.get_rates method.

Payload

The request payload contains information about all items in the cart, including product information, product attributes, item custom options, shipping address, and customer information for logged-in customers.

Payload example:

{
    "rateRequest": {
        "all_items": [
            {
                "item_id": "1",
                "quote_id": "1",
                "created_at": "2025-02-18 14:57:03",
                "updated_at": "2025-02-24 03:44:49",
                "product_id": "9",
                "store_id": 1,
                "parent_item_id": null,
                "is_virtual": "0",
                "sku": "simple-product-1",
                "name": "Simple product 1",
                "price": 500,
                "base_price": 500,
                ...
                "product": {
                    "entity_id": "9",
                    "attribute_set_id": "4",
                    "type_id": "simple",
                    "sku": "simple-product-1",
                    "price": "500.000000",
                    ...
                    "attributes": {
                        "manufacturer": "Demo Company",
                        "color": "Red",
                        "country_origin": "Japan",
                        ...
                    }
                },
                "custom_options": [
                  {
                    "label": "Monogram",
                    "option_id": "1",
                    "option_value": "J.D."
                  },
                  ...
                ]
            }
        ],
        "dest_country_id": "US",
        "dest_region_id": 12,
        "dest_region_code": "CA",
        "dest_street": "3320 N Crescent Dr\nBeverly Hills",
        "dest_city": "Los Angeles",
        "dest_postcode": "70210",
        "package_value": 1100,
        "package_value_with_discount": 1100,
        "package_weight": 124,
        "package_qty": 2,
        "package_physical_value": 1100,
        "free_method_weight": 124,
        "store_id": 1,
        "website_id": "1",
        "free_shipping": 0,
        "base_currency": {
            "currency_code": "USD"
        },
        "package_currency": {
            "currency_code": "USD"
        },
        ...
        "customer": {
            "entity_id": "1",
            "website_id": "1",
            "email": "test@example.com",
            "group_id": "1",
            "firstname": "John",
            "middlename": null,
            "lastname": "Doe",
            ...
        },
        "selected_shipping_method": {
            "carrier_code": "DPS",
            "method_code": "dps_shipping_one"
        },
        "address_custom_attributes": {
            "custom_attribute_one": "value_one",
            ...
        }
    }
}

The request payload also contains information about the selected shipping method, if it was already selected before webhook execution. When no shipping method has been selected yet, the selected_shipping_method field is null.

Additionally, the request payload contains custom attributes for the shipping address. See Customer address attributes for information on creating custom address attributes.

You can find examples of how to use shipping addresses, customer data, and product attributes in your App Builder application in actions/shipping-methods.js.

GraphQL

In the setShippingAddressesOnCart mutation, available shipping methods that are returned by the webhook are appended to the available_shipping_methods field.

You can use the additional_data field to pass an array of key-value pairs to provide additional information about the shipping method from the webhook.

{
  "data": {
    "setShippingAddressesOnCart": {
      "cart": {
        "shipping_addresses": [
          {
            "firstname": "John",
            "lastname": "Doe",
            "company": "Company Name",
            "street": ["3320 N Crescent Dr", "Beverly Hills"],
            "city": "Los Angeles",
            "region": {
              "code": "CA",
              "label": "California"
            },
            "postcode": "70210",
            "telephone": "123-456-0000",
            "country": {
              "code": "US",
              "label": "US"
            },
            "available_shipping_methods": [
              {
                "amount": {
                  "currency": "USD",
                  "value": 17
                },
                "available": true,
                "carrier_code": "DPS",
                "carrier_title": "Demo Postal Service",
                "error_message": "",
                "method_code": "dps_shipping_one",
                "method_title": "Demo Custom Shipping One",
                "price_excl_tax": {
                  "currency": "USD",
                  "value": 17
                },
                "price_incl_tax": {
                  "currency": "USD",
                  "value": 17
                },
                "additional_data": [
                  {
                    "key": "additional_data_key",
                    "value": "additional_data_value"
                  },
                  {
                    "key": "additional_data_key2",
                    "value": "additional_data_value2"
                  },
                  {
                    "key": "additional_data_key3",
                    "value": "additional_data_value3"
                  }
                ]
              },
              {
                "amount": {
                  "currency": "USD",
                  "value": 18
                },
                "available": true,
                "carrier_code": "DPS",
                "carrier_title": "Demo Postal Service",
                "error_message": "",
                "method_code": "dps_shipping_two",
                "method_title": "Demo Custom Shipping Two",
                "price_excl_tax": {
                  "currency": "USD",
                  "value": 18
                },
                "price_incl_tax": {
                  "currency": "USD",
                  "value": 18
                },
                "additional_data": []
              }
            ]
          }
        ]
      }
    }
  }
}

In the setShippingMethodsOnCart mutation, you can set the shipping method provided by webhook. Its information is stored in the selected_shipping_method field with the additional_data, if provided.

{
  "data": {
    "setShippingMethodsOnCart": {
      "cart": {
        "shipping_addresses": [
          {
            "selected_shipping_method": {
              "amount": {
                "currency": "USD",
                "value": 17
              },
              "carrier_code": "DPS",
              "carrier_title": "Demo Postal Service",
              "method_code": "dps_shipping_one",
              "method_title": "Demo Custom Shipping One",
              "price_excl_tax": {
                "currency": "USD",
                "value": 17
              },
              "price_incl_tax": {
                "currency": "USD",
                "value": 17
              },
              "additional_data": [
                {
                  "key": "additional_data_key",
                  "value": "additional_data_value"
                },
                {
                  "key": "additional_data_key2",
                  "value": "additional_data_value2"
                },
                {
                  "key": "additional_data_key3",
                  "value": "additional_data_value3"
                }
              ]
            }
          }
        ]
      }
    }
  }
}

Remove shipping method

The plugin.magento.out_of_process_shipping_methods.api.shipping_rate_repository.get_rates webhook allows you to remove specific shipping methods from the list of available options.

If you use the flatrate shipping method, but want to disable it, you must update your webhook response to mark the shipping method as removed. This example is demonstrated in actions/shipping-methods.js.

Filter shipment items

The plugin.magento.out_of_process_shipping_methods.api.shipment_item_filter.filter webhook lets you exclude order items from new shipments. For example, you can keep items that a third party fulfills out of Commerce shipments.

This use case runs with a webhook of type after.

Webhook name

PaaS Only plugin.magento.out_of_process_shipping_methods.api.shipment_item_filter.filter

SaaS Only plugin.out_of_process_shipping_methods.api.shipment_item_filter.filter

The webhook is called when a shipment is created in one of the following ways:

Shipments created in other ways, including through the POST /V1/shipment endpoint, are not filtered.

The webhook only changes the contents of the shipment. It does not change order items or invoiced quantities, and excluded quantities remain available for a later shipment.

PaaS Only This webhook requires version 1.3.0 or later of the magento/module-out-of-process-shipping-methods module.

Configuration

data-slots=heading, code
data-repeat=2
data-languages=XML, YAML

webhooks.xml (PaaS)

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_AdobeCommerceWebhooks:etc/webhooks.xsd">
  <method name="plugin.magento.out_of_process_shipping_methods.api.shipment_item_filter.filter" type="after">
    <hooks>
      <batch name="shipment_item_filter">
        <hook name="exclude_third_party_items" url="https://<your_app_builder>.runtime.adobe.io/api/v1/web/shipping-method/shipment-item-filter" method="POST" timeout="5000" softTimeout="1000" required="true">
          <fields>
            <field name="payload" />
            <field name="result" />
          </fields>
        </hook>
      </batch>
    </hooks>
  </method>
</config>

Admin (SaaS)

Hook Settings

  Webhook Method: plugin.out_of_process_shipping_methods.api.shipment_item_filter.filter
  Webhook Type: after
  Batch Name: shipment_item_filter
  Hook Name: exclude_third_party_items
  URL: https://<your_app_builder>.runtime.adobe.io/api/v1/web/shipping-method/shipment-item-filter
  Timeout: 5000
  Soft timeout: 1000
  Required: Required
  Active: Yes
  Method: POST

Hook Fields

  Name: payload
  Source: payload
  Active: Yes

  Name: result
  Source: result
  Active: Yes

Whether a failed webhook call blocks the shipment depends on the Required setting of the webhook.

Request payload

The request contains the shipment creation context, the order, and the items requested to be shipped. The result field contains the IDs of order items that other webhooks subscribed to the same method have already excluded. It is empty otherwise.

{
  "payload": {
    "context": {
      "channel": "api",
      "source_code": "default",
      "caller_type": "integration",
      "caller_id": 7
    },
    "order": {
      "entity_id": 100,
      "increment_id": "000000100",
      "store_id": 1,
      "status": "processing"
    },
    "items": {
      "101": {
        "order_item_id": 101,
        "product_id": 42,
        "sku": "regular-item",
        "name": "Regular item",
        "product_type": "simple",
        "parent_item_id": null,
        "qty": 2,
        "qty_ordered": 2,
        "qty_shipped": 0,
        "source_code": "default",
        "custom_attributes": [
          {
            "attribute_code": "vendor",
            "value": "acme"
          }
        ]
      }
    }
  },
  "result": []
}

The payload.context object describes how the shipment is being created:

Field
Type
Description
channel
String
How the shipment is being created: admin_shipment, admin_invoice, or api.
source_code
String
The source requested for the whole shipment: the source selected in the Admin, or the source_code extension attribute of the arguments object in the POST /V1/order/{orderId}/ship request. null if no source was requested.
caller_type
String
The type of the authenticated caller: integration, admin, customer, or guest. null if the caller is unknown, for example, when a message queue consumer processes an asynchronous request.
caller_id
Integer
The ID of the authenticated caller, such as the integration ID or Admin user ID. null if the caller is unknown.

The payload.order object describes the order that the shipment is created for:

Field
Type
Description
entity_id
Integer
The order ID.
increment_id
String
The order increment ID.
store_id
Integer
The ID of the store the order was placed in.
status
String
The order status.

The payload.items object is keyed by order item ID. It contains the non-virtual items with a positive requested quantity. For composite products, it also contains the parent items of the requested child items. Each item contains the following fields:

Field
Type
Description
order_item_id
Integer
The order item ID.
product_id
Integer
The product ID. null if the product no longer exists.
sku
String
The product SKU.
name
String
The product name.
product_type
String
The product type, such as simple, configurable, or bundle.
parent_item_id
Integer
The ID of the parent order item. null for a top-level item.
qty
Float
The quantity requested to be shipped.
qty_ordered
Float
The ordered quantity.
qty_shipped
Float
The quantity that has already been shipped.
source_code
String
The source selected for the shipment. null if no source was selected.
custom_attributes
Array
The custom attributes of the order item, as a list of attribute_code and value pairs. Empty if the order item has no custom attributes.

SaaS Only If a source was nominated for an order item, the source_code field of the item contains the nominated source instead of the source selected for the shipment. Items without a nominated source keep the source selected for the shipment.

Response

To exclude an item, return an add operation that adds its order item ID to result:

[
  {
    "op": "add",
    "path": "result",
    "value": 101
  }
]

Return one add operation for each excluded item. To exclude nothing, return a success operation. To block the shipment, return an exception operation. For the api channel, the exception message is returned to the caller.

The following rules apply to the returned IDs:

Shipment behavior

The result of the webhook depends on how the shipment is being created:

Case
Submit Shipment
Submit Invoice with Create Shipment
POST /V1/order/{orderId}/ship
Some items are excluded
The other items are shipped. A notice lists the excluded SKUs.
Same as Submit Shipment.
No shipment is created. The request fails with an error that lists the excluded SKUs.
Only a child item is excluded, without its parent item
The exclusion is ignored and logged. All items are shipped without a notice.
Same as Submit Shipment.
The exclusion is ignored and logged. The shipment is created with all items.
Some items are excluded and the other items were already shipped
Not applicable.
The invoice is created without a shipment. A warning is displayed.
No shipment is created. The request fails with an error that lists the excluded SKUs.
All items are excluded
No shipment is created. An error is displayed.
The invoice is created without a shipment. A warning is displayed.
No shipment is created. The request fails with an error that lists the excluded SKUs.
The webhook fails or returns an exception operation
No shipment is created. The error is displayed.
The invoice is created without a shipment. A warning is displayed.
No shipment is created. The request fails with the error.

REST API callers never receive a shipment that is missing requested items. They can retry the request without the excluded items.

When the invoice is created without a shipment, the Admin displays the message The invoice has been created. instead of You created the invoice and shipment.