Claudio Ferraro
Claudio Ferraro

Reputation: 4721

After creation of custom shipping method the getShippingCarrier() function returns nothing why?

I followed this guide:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/shipping/create-shipping-method-module

and created a custom shipping method. But as I call this function:

getShippingCarrier() from the Mage_Sales_Model_Order  (collection Sales/Order)

on the orders created with my custom shipping method, I get nothing. Cannot figure out why this happens.

Upvotes: 2

Views: 326

Answers (2)

Manuel Caro
Manuel Caro

Reputation: 46

Same problem here.

It solved my problem (https://www.facebook.com/scandiweb/posts/788413034530650):

It´s an interesting limitation in Magento. Namely – when creating new shipping method don’t use underscores in carrier or method code. Underscore in code has potential to break any code that uses this method – Mage_Sales_Model_Order::getShippingMethod. As an example I found out myself – I was missing shipping carrier in exported orders for certain shipping methods. Not cool at all if this happens live. The reason lies here in Mage_Sales_Model_Order::getShippingMethod

public function getShippingMethod($asObject = false)
{
  $shippingMethod = parent::getShippingMethod();
  if (!$asObject) {
    return $shippingMethod;
  } else {
    list($carrierCode, $method) = explode('_', $shippingMethod, 2);
    return new Varien_Object(array(
      'carrier_code' => $carrierCode,
      'method' => $method
    ));
  }
}

Notice the explode function call. It takes string that looks something like ‘carriercode_methodcode’ and splits into parts by underscore. If however there are some additional underscores like in ‘carrier_code_method_code’ it will get wrong codes and for example getShippingCarrier() will fail to return carrier that was used on order.

Upvotes: 3

komodosp
komodosp

Reputation: 3616

I had the same problem as you but for different reasons. What I notice in the guide you refered to was this code was missing from the config.xml file

<default>
    <carriers>
        <saturdaydelivery>
            <active>1</active>
            <model>saturdaydelivery/carrier_saturdaydelivery</model>
            <title>Saturday Delivery</title>
            <sort_order>10</sort_order>
            <sallowspecific>0</sallowspecific>
        </saturdaydelivery>
    </carriers>
</default>

It would go outside the <globals>...<\globals> element, after it I suppose (I have it as the last code before </config> but I have no idea if that matters). The <model> tag is the important one in this case as that's what the getShippingCarrier() function goes looking for.

BTW, "saturdaydelivery" was what I had instead of "newmodule" as described in the guide. (In case it wasn't obvious! ;) )

Upvotes: 1

Related Questions