How to use commerce_shipping hooks I'm trying to construct a shipping module for UK's Royal Mail (or at least the parts I need right now...!).  I've written PHP code for calculating what methods are applicable based on shipping address and weight, but plumbing this logic into my site is eluding me.  I should add that, as Royal Mail doesn't have an API for pricing, my module bundles the rate-tables for the various methods/services (CSV format) so I need to use code, not Rules, to implement it.

It seems the way to do this is to use the hook_commerce_shipping_* hooks, but it doesn't appear that I'm doing so correctly.

This is my first experience with using hooks (and Drupal on a serious dev level, TBH) but I have successfully implemented hook_commerce_cart_product_prepare() in another custom module, so I think what I'm doing should be working.  In my module I wrote the following, just to test that I'd successfully implemented the hook:

/**
 * Implements hook_commerce_shipping_calculate_rate()
 */
function royalmail_commerce_shipping_calculate_rate($line_item) {
    drupal_set_message(print_r($line_item, TRUE),'error');
}


...and it seems I hadn't, because the message never appeared when I progressed to the shipping pane of the checkout.

I also tried various other "canaries" I should be able to spot evidence of, including but not limited to drupal_exit(), echo, die(), var_dump() and so on, but nothing appears.  Same story for hook_commerce_shipping_collect_rates(), which I actually tried to implement first but ended up doing so with Rules instead while I was scratching my head (though I'd prefer this be in native code too, ideally).

Am I doing it wrong? Thanks in advance.
It looks like you are using commerce shipping 2.x which differs a lot from 1.x in terms of integration.

What you need to do is:


Define a shipping method, use hook_commerce_shipping_method_info.
Define one or more shipping services use hook_commerce_shipping_service_info
When you define the shipping services, you can select a rate callback, if none is selected, commerce shipping will by default try to use SERVICE_NAME_rate, where SERVICE_NAME is the keyed name used in the hook_commerce_shipping_service_info hook. The rate callback have this signature:

function service_name_rate($shipping_service, $order) {
  // Logic here.
  return array(
    'amount' =&gt; $amount,
    'currency_code' =&gt; $currency_code,
    'data' =&gt; array(),
  );
}



You can look at the example module within the commerce shipping module which serves as documentation, or you can look at some of the other shipping methods, like UPS.