Magento 2 Custom shipping Carrier module

Magento 2 Custom shipping Carrier module

September 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 Custom shipping Carrier module

Solving Magento 2 Custom Shipping Carrier Module Challenges

Hello developers! Today we’re going to dive into a common challenge faced by those new to Magento 2 - adding a custom shipping carrier using an API with multiple options based on product dimensions.

Understanding the Challenge: Multiple Shipping Options

You’ve already made significant progress by following the excellent guide from Inchoo on creating a shipping method in Magento 2. However, you’re now facing an issue where multiple options aren’t being saved correctly, leading to the error message “Carrier with such method not found”.

The Current Approach: Looping Through Options

Let’s first look at your current code snippet:

for ($n = 0; $n < count($optionz); $n++) {
    $method = $this->rateMethodFactory->create();
    $method->setMethod($this->_code."_".$n);
    $method->setCarrier($this->_code);
    $method->setCarrierTitle($optionz[$n]['service']);
    $method->setCost($optionz[$n]['fee']);
    $method->setPrice($optionz[$n]['fee']);
    $result->append($method);
}

This approach is correct in that it loops through your options array, creates a new shipping method for each one, and adds them to the result object. However, the issue lies in how Magento 2 processes these methods.

The Root Cause: Method Naming and Indexing

The problem arises because you’re using an index $n in your method name ($this->_code."_".$n). While this works during the loop, it’s causing issues when Magento tries to retrieve the method later. It expects a consistent naming convention across all methods from a single carrier.

The Solution: Using Consistent Method Names

To resolve this, we need to ensure that each shipping method has a unique, static name that doesn’t include an incrementing index. Here’s how you can modify your loop:

foreach ($optionz as $index => $option) {
    $method = $this->rateMethodFactory->create();
    // Use a consistent base name for all methods from the carrier
    $methodNameBase = 'my_custom_carrier_method';
    $method->setMethod($methodNameBase . '_' . $option['service']);
    $method->setCarrier($this->_code);
    $method->setCarrierTitle($option['service']);
    $method->setCost($option['fee']);
    $method->setPrice($option['fee']);
    $result->append($method);
}

In this revised code:

  • We use a foreach loop with the index $index, which eliminates the need for an incrementing counter.
  • We define a base method name my_custom_carrier_method. This should be unique to your carrier and consistent across all methods.
  • We then concatenate the base name with the service option title as the final method name.

Conclusion: Ensuring Method Consistency

By following this approach, you ensure that each shipping method is assigned a unique yet consistent name. This allows Magento 2 to correctly identify and process your custom shipping options during checkout.

Remember, when working with Magento’s core functionality like carriers and shipping methods, consistency in naming conventions is key for avoiding errors and smooth operations.

For more advanced tips on Magento 2 development, don’t forget to check out our blog at LaravelCompany. We cover a wide range of topics from beginner tutorials to expert-level guides.

Happy coding!

Magento 2 Catalog Indexing API

Our network