Zachary Schuessler
Zachary Schuessler

Reputation: 3682

Checking If Order is Being Edited By Admin?

My custom shipping module was failing because it couldn't get a sales quote when an order was being edited on the backend. This was the code I was using:

class Mymodule_Model_Mycarrier_Customrate 
    extends Mage_Shipping_Model_Carrier_Abstract
        implements Mage_Shipping_Model_Carrier_Interface
{
    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        $quote = Mage::getSingleton('checkout/type_onepage')->getQuote();

I need to get the current quote so I have access to the address information. I am making an API request that requires a street address.

Now if the order was being edited on the backend, this would obviously result in an error because the checkout singleton is no longer relevant. Instead I get the quote like this:

$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote();

In my collectRates() method I need to determine which singleton to load. First I want to ask if this is the proper way of doing things, and also if my check for the backend is sufficient:

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

// If admin is editing an order, find the quote by admin session.
if(Mage::getSingleton('admin/session')->isLoggedIn()){
    $quote = Mage::getSingleton('adminhtml/session_quote')->getQuote();
}

I don't want this to potentially cause problems later. I also get the feeling that I may be using the collectRates() method wrong if I have to create a workaround like this.

Upvotes: 3

Views: 757

Answers (1)

Joe Constant
Joe Constant

Reputation: 822

The Mage_Shipping_Model_Rate_Request object already has access to the address via $request->getDestStreet(), etc...

Upvotes: 1

Related Questions