Magento Set Shipping cost when create order programmatically

Magento Set Shipping cost when create order programmatically

August 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Set Shipping cost when create order programmatically

Setting Shipping Cost When Creating Orders Programmatically in Magento

Setting shipping costs programmatically in Magento can be a challenging task, but it is essential for many e-commerce businesses that import orders from external feeds or have custom order creation processes. Let’s dive into the issue described and provide a step-by-step solution.

Understanding the Current Code Structure

The provided code is attempting to set the shipping method and amount while creating an order programmatically in Magento 1.7.0.2 CE:

$shippingAddress = $quote->getShippingAddress()->addData($addressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
        ->setShippingMethod('customname_customname')
        ->setShippingAmount('10')
        ->setBaseShippingAmount('10');
$quote->collectTotals();
$quote->save();

The $addressData variable contains customer information, and the code attempts to set a custom shipping method with an amount of $10. However, this approach seems to fall short as the shipping cost is not being set correctly.

Identifying the Problem

The issue lies primarily in the custom carrier module’s implementation:

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    // Implementation details removed for brevity
}

public function getAllowedMethods()
{
    return array($this->_code => $this->getConfigData('name'));
}

In the collectRates method, the shipping price and cost are set to ‘0.00’ regardless of the actual calculation or external data provided by the $request. Additionally, the custom carrier does not perform any calculations based on the quote’s data.

The Correct Approach

To correctly set the shipping cost programmatically in Magento, you should follow these steps:

  1. Update the Custom Carrier Implementation

In your custom carrier module, update the collectRates method to calculate the shipping price and cost based on relevant data from the $request:

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    // Calculate shipping price based on request data (e.g., weight, destination, etc.)
    $shippingPrice = calculateShippingPrice($request->getPackageWeight(), $request->getDestCountryId());

    // Update the rate result
    $result = Mage::getModel('shipping/rate_result');
    $method = Mage::getModel('shipping/rate_result_method');

    // Set carrier and method details

    // Set calculated shipping price and cost
    $method->setPrice($shippingPrice);
    $method->setCost($shippingPrice);

    // Append the method to the result
    $result->append($method);

    return $result;
}
  1. Use the Correct Shipping Method in Your Code

Ensure that you are using the correct shipping method code when setting the shipping details:

$shippingAddress = $quote->getShippingAddress()->addData($addressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
        ->setShippingMethod('customname') // Update this to match your carrier's _code
        ->setShippingAmount($shippingPrice)
        ->setBaseShippingAmount($shippingPrice);

$quote->collectTotals();
$quote->save();
  1. Update Your Custom Shipping Method Configuration

Make sure that your custom shipping method is properly configured in the Magento admin interface, including setting the title and name accurately.

Conclusion

Setting shipping costs programmatically in Magento requires a proper implementation of the custom carrier module’s collectRates method and using the correct shipping method code. By calculating the shipping price based on relevant data from the request and updating your custom carrier accordingly, you can successfully set the shipping cost when creating orders programmatically.

Remember to test your changes thoroughly after implementing them in your Magento installation.

For more detailed information on working with Magento’s shipping functionality, consider exploring our guide to Magento Shipping Extensions on Laravel Company.

Magento 1 Extensions Configuration Orders

Our network