Magento – How to show fedex estimated delivery date to be displayed along with the shipping option on the checkout page

checkoutdatedeliveryfedex

Currently in checkout process, in shipping method section Fedex Shipping method showing as –

Federal Express

Ground - $13.86
Standard Overnight - $36.87
2 Day - $36.87

I would like the estimated delivery date to be displayed along with the shipping option on my checkout page.

Federal Express

Ground - Est. Delivery February 21, 2016 $13.86
Standard Overnight - Est. Delivery February 21, 2016 $36.87
2 Day - Est. Delivery February 21, 2016 $36.87

One more thing "Is Fedex return delivery date ?" if yes "how can i get the delivery date ?".

Best Answer

Yes, Fedex returns delivery date but you have to configure the request sent to the api accordingly.

I am not sure your version of magento, This will work in Magento 1.9.x.

  1. Rewrite "Mage_Usa_Model_Shipping_Carrier_Fedex" method "_formRateRequest"
  2. add 'ReturnTransitAndCommit'=>true to the "ratesRequest" array,

    ...
    $ratesRequest = array(
        'ReturnTransitAndCommit'=>true,
    ....
    

    Now if you log the response in "_prepareRateResponse", you will see something similar to

    [RateReplyDetails] => Array
    (
        [0] => stdClass Object
            (
                [ServiceType] => FIRST_OVERNIGHT
                [PackagingType] => YOUR_PACKAGING
                [DeliveryStation] => KCKA 
                [DeliveryDayOfWeek] => FRI
                [DeliveryTimestamp] => 2016-02-19T08:00:00
                [CommitDetails] => stdClass Object
                    (
                        [ServiceType] => FIRST_OVERNIGHT
                        [CommitTimestamp] => 2016-02-19T08:00:00
                        [DayOfWeek] => FRI
                        [DestinationServiceArea] => A1
                        [BrokerToDestinationDays] => 0
                        [DocumentContent] => NON_DOCUMENTS
                    )
    
                [DestinationAirportId] => MCI
             ....
    

Notice

                [DeliveryDayOfWeek] => FRI
                [DeliveryTimestamp] => 2016-02-19T08:00:00

To get the timestamp to the checkout page, override "_prepareRateResponse" method by the following code (change is marked as - checkpoint)

protected function _prepareRateResponse($response)
{
    $costArr = array();
    $priceArr = array();
    $errorTitle = 'Unable to retrieve tracking';

    if (is_object($response)) {
        if ($response->HighestSeverity == 'FAILURE' || $response->HighestSeverity == 'ERROR') {
            if (is_array($response->Notifications)) {
                $notification = array_pop($response->Notifications);
                $errorTitle = (string)$notification->Message;
            } else {
                $errorTitle = (string)$response->Notifications->Message;
            }
        } elseif (isset($response->RateReplyDetails)) {
            $allowedMethods = explode(",", $this->getConfigData('allowed_methods'));

            if (is_array($response->RateReplyDetails)) {
                foreach ($response->RateReplyDetails as $rate) {
                    $serviceName = (string)$rate->ServiceType;
                    if (in_array($serviceName, $allowedMethods)) {
                        $amount = $this->_getRateAmountOriginBased($rate);
                        $costArr[$serviceName]  = $amount;
                        $priceArr[$serviceName] = $this->getMethodPrice($amount, $serviceName);
                        $timestamp[$serviceName] = (string) $rate->DeliveryTimestamp;//checkpoint
                    }
                }
                asort($priceArr);
            } else {
                $rate = $response->RateReplyDetails;
                $serviceName = (string)$rate->ServiceType;
                if (in_array($serviceName, $allowedMethods)) {
                    $amount = $this->_getRateAmountOriginBased($rate);
                    $costArr[$serviceName]  = $amount;
                    $priceArr[$serviceName] = $this->getMethodPrice($amount, $serviceName);
                }
            }
        }
    }

    $result = Mage::getModel('shipping/rate_result');
    if (empty($priceArr)) {
        $error = Mage::getModel('shipping/rate_result_error');
        $error->setCarrier($this->_code);
        $error->setCarrierTitle($this->getConfigData('title'));
        $error->setErrorMessage($errorTitle);
        $error->setErrorMessage($this->getConfigData('specificerrmsg'));
        $result->append($error);
    } else {
        foreach ($priceArr as $method=>$price) {
            $rate = Mage::getModel('shipping/rate_result_method');
            $rate->setCarrier($this->_code);
            $rate->setCarrierTitle($this->getConfigData('title'));
            $rate->setMethod($method);
            $rate->setMethodTitle($this->getCode('method', $method));
            $rate->setCost($costArr[$method]);
            $rate->setPrice($price);
            $rate->setDeliveryTimeStamp($timestamp[$method]);//checkpoint
            $result->append($rate);
        }
    }
    return $result;
}

Rewrite "Mage_Sales_Model_Quote_Address_Rate" as

 public function  importShippingRate(Mage_Shipping_Model_Rate_Result_Abstract $rate)
{
    $rateData = parent::importShippingRate($rate);
    if ($rate instanceof Mage_Shipping_Model_Rate_Result_Method) {
          $rateData->setDeliveryTimeStamp($rate->getDeliveryTimeStamp());
    }
    return $rateData;
}

Go to desing/frontend/[package]/[theme]/checkout/onepage/shipping_method.phtml and call the time stamp as

         ...
        foreach ($_rates as $_rate): 
          ...
         echo  $_rate->getDeliveryTimeStamp();
          ....
      endforeach;

You can format the the timestamp using php methods.

I hope this helps!