0

I am trying to set Shipping Method Progrmatically but getting following error. The shipping method is missing. Select the shipping method and try again.

foreach ($quote->getAllAddresses() as $address) {
            // Build grand total.
        $grandTotal = (($finalPrice + $shipping + $tax) - $discount);
        $baseGrandTotal =  $this->convertPrice($grandTotal);
        $baseFinalPrice =  $this->convertPrice($finalPrice);
        $baseDiscount =  $this->convertPrice($discount);
        $baseTax =  $this->convertPrice($tax);

        //$address->setFreeShipping(true); //tried this but not getting set

        $address->setShippingMethod('flatrate_flatrate'); //tried this getting error
        $address->setCollectShippingRates(true);


        $address->setBaseSubtotal($baseFinalPrice);
        $address->setSubtotal($finalPrice);
        $address->setDiscountAmount($discount);
        $address->setBaseDiscountAmount($baseDiscount);
        $address->setTaxAmount($tax);
        $address->setBaseTaxAmount($baseTax);
        $address->setBaseGrandTotal($baseGrandTotal);
        $address->setGrandTotal($grandTotal);
        $address->save();

}
  • First, you have to calculate the total then you have to check the available shipping method and then apply Shipping method. – Amit Bera Aug 19 at 13:58
0

Please update the code like below

$address->setShippingMethod('flatrate_flatrate'); 
$address->setCollectShippingRates(true)->collectShippingRates();

Let me know if you need further assistance.

  • Still same error – Sairaj Gadekar Aug 19 at 14:40
  • @ramkrishan if i try to use setshippingmethod and then if i use getshippingmethod the result is empty – Sairaj Gadekar Aug 22 at 9:25
0

Please update this code :-

 $shippingAddress = $quote->getShippingAddress();

 $shippingAddress->setFreeShipping(true);
 $shippingAddress->setShippingMethod('flatrate_flatrate');
 $shippingAddress->setCollectShippingRates(true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.