Magento available shipping methods by order id
Magento available shipping methods by order id

Here I am going to show you how to get Magenot available shipping methods by order id.I am writing this article because I was facing problem for doing same type of requirements.The requirement is I have order object where I can get order Id easily and I am not in checkout page or quote section .I Want to fetch all available shipping method for an order in order view page.SO for that I have only order object and all sorts of order items.But I don’t have quote items.But How can I fetch that quote logic in order info page of admin.So That admin can change shipping method if he want to change shipping method of an existing order.For all requirements a custom module be required but I can just show you how you can only fetch Magento available shipping methods by order id.Then all other logic is yours.Like to show block in order info page and to place a new button called change shipping method etc.But I am just explains how you can do some of parts of my that custom module.

So simply you can get order object in order info page by

public function getOrder() {
    if (is_null($this->order)) {
        if (Mage::registry('current_order')) {
            $order = Mage::registry('current_order');
        }
        elseif (Mage::registry('order')) {
            $order = Mage::registry('order');
        }
        else {
            $order = new Varien_Object();
        }
        $this->order = $order;
    }
    return $this->order;
}

or if you have order object then its better to get order Id.In my case I just fetch the order object by $this->getOrder().

$order = $this->getOrder();
$orderId = $order->getId();

Now Our logic is how get all rate request.

public function getRateRquestByOrder($orderId, $limitCarrier = null){
    $order = Mage::getModel('sales/order')->load($orderId);
    $address = $order->getShippingAddress();
    $country = $address->getCountryId();
    $items = $order->getAllItems();
    
    $request = Mage::getModel('shipping/rate_request');
    $request->setAllItems($items);
    $request->setDestCountryId($address->getCountryId());
    $request->setDestRegionId($address->getRegionId());
    $request->setDestPostcode($address->getPostcode());
    $request->setPackageValue($order->getBaseSubtotal());
    $request->setPackageValueWithDiscount($order->getBaseSubtotalWithDiscount());
    $request->setPackageWeight($order->getWeight());
    $request->setFreeMethodWeight(0);
    $request->setPackagePhysicalValue($order->getGrandTotal());
    $request->setPackageQty($order->getItemQty());
    $request->setStoreId($order->getStoreId());
    $request->setWebsiteId($order->getWebsiteId());
    $request->setBaseCurrency($order->getBaseCurrencyCode());
    $request->setPackageCurrency($order->getOrderCurrencyCode());
    $request->setLimitCarrier($limitCarrier);

    $request->setBaseSubtotalInclTax($order->getBaseSubtotalInclTax());
    
    $result = Mage::getModel('shipping/shipping')->collectRates($request)->getResult();
    
    if ($result) {
        $shippingRates = $result->getAllRates();
        $allShippingRates = array();
        foreach ($shippingRates as $key => $shippingRate) {
            $allShippingRates[] = $shippingRate->getData();
        }
        return $allShippingRates;
    }else{
        reurn array();
    }
}

You can simply call to the above function by

$allShippingRates = $this->getRateRquestByOrder($order->getId());
Magento available shipping methods by order id
Tagged on:                         

One thought on “Magento available shipping methods by order id

Leave a Reply

Your email address will not be published. Required fields are marked *