1

I have make some change in file to make it selected but i don't think this is the right to make it working like this.so i just want to know from the master that is there any other way to do it using magento standard code

I have rewrite the checkout controller estimatepostaction.

Here is below code.

public function estimatePostAction()
{
    /*
     * if the quantity in the cart is updated, then the estimate address is updated
     * to the changed address
     * if the quantity is not updated, then the estimate address is set to the default
     * one available
     *
     */
    $country = (string) $this->getRequest()->getParam('country_id');
    $postcode = (string) $this->getRequest()->getParam('estimate_postcode');
    $city = (string) $this->getRequest()->getParam('estimate_city');
    $regionId = (string) $this->getRequest()->getParam('region_id');
    $region = (string) $this->getRequest()->getParam('region');

    $quote = $this->_getCart()->getQuote();

    $quote->getShippingAddress()
        ->setCountryId($country)
        ->setPostcode($postcode)
        ->setRegionId($regionId)
        ->setCollectShippingRates(false)
        ->collectShippingRates();
    $this->_getCart()->save();

    $cartShippings = [];
    $allRates = $quote->getShippingAddress()
        ->collectShippingRates()
        ->getGroupedAllShippingRates();
    foreach ($allRates as $carrier) {
        foreach ($carrier as $rate) {
            $cartShippings[] = $rate->getData();
        }
    }

    // This code calculates the lowest shipping amount and the shipping code associated
    // with it.

    $totalShippingMethods = count($cartShippings);
    $minValue = (float) 999999;
    $lowestShipingCode = '';
    for ($smallVal = 0; $smallVal < $totalShippingMethods; $smallVal ++) {
        $current_rate = (float) $cartShippings[$smallVal]['price'];
        if ($current_rate < $minValue) {
            $minValue = $cartShippings[$smallVal]['price'];
            $lowestShipingCode = $cartShippings[$smallVal]['code'];
        }
    }

    $leastShippingCode = (string) $lowestShipingCode;

    // setting the shipping code to the quote.
    if (! empty($leastShippingCode)) {
        $this->_getQuote()
            ->getShippingAddress()
            ->setShippingMethod($leastShippingCode)/*->collectTotals()*/->save();
    }
    $this->_goBack();
}

Your Answer

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

Browse other questions tagged or ask your own question.