Magento – How to Add Shipping Rate While Creating Order Programmatically

create-orderorderssales-ordershipping

Shipping Method addition is happening as it should be. But, Shipping Rate as per the Shipping method or, custom Shipping rate is not getting added to the order.

$address = $quoteObj->getShippingAddress ();
$address->setCollectShippingRates ( true )->collectShippingRates()
        ->setShippingMethod('flatrate_flatrate');
$quoteObj->collectTotals ()->save ();

Searching through some other questions I found a solution which adds Custom Shipping Rate to the order, but it doesn't add the shipping rates to Grand Total.

$shippingprice = 0.9;
$orderObj->setShippingAmount($shippingprice);
$orderObj->setBaseShippingAmount($shippingprice);

enter image description here

Best Answer

Try this:

$shippingprice = 0.9;
$orderObj->setShippingAmount($shippingprice);
$orderObj->setBaseShippingAmount($shippingprice);
$orderObj->setGrandTotal($orderObj->getGrandTotal() + $shippingprice); //adding shipping price to grand total
$orderObj->save();
Related Topic