Search code examples
phpmagento

Magento 1.7 Getting order data form Mage_Shipping_Model_Rate_Request


How can I get order info (like a Shipping method and payment info) in collectRates on my custom shipping module?

I think it can be gotten from the Mage_Shipping_Model_Rate_Request, but I do not know how.


Solution

  • You cannot get order data in collectRates because the order is not created yet. You can get the quote info.
    But I doubt you will be able to get the shipping method or payment info because collectRates is being called when the checkout is trying to display the shipping methods step in the checkout.
    So usually you don't have the shipping method set or the payment method...unless someone already filled that in and refreshed the page.
    You can access the quote like this:
    The collectRates method receives a parameter called $request like you said.
    Do this:

    $quote = false;
    foreach ($request->getAllItems() as $item){
        $quote = $item->getQuote();
        break;
    }
    if ($quote) {
         $shippingMethod = $quote->getShippingAddress()->getShippingMethod();
         $payment = $quote->getPayment();
    }
    

    You can also have access to the quote anywhere like this:

    $quote = Mage::getSingleton('checkout/session')->getQuote(); 
    

    and work with the methods above. The only downside of this last approach is that it does not work in the backend.