Drupal Commerce: Disabling a checkout pane based on the contents of the order

Yesterday I was asked if I could investigate how to disable a Drupal Commerce checkout pane depending on the contents of the order. Having never used Drupal Commerce before yesterday the task seemed a bit overwhelming. Thanks to good documentation and help from #drupal-commerce on IRC, I came up with the following.

Summary

  1. Create a secondary customer profile pane with hook_commerce_customer_profile_type_info().
  2. Look at the order and choose to disable the custom customer profile checkout pane using hook_commerce_checkout_pane_info_alter().

Code

custom_commerce.module:

<?php
/**
 * @file
 * Custom commerce module file.
 */

/**
 * Implements hook_commerce_customer_profile_type_info().
 */
function custom_commerce_commerce_customer_profile_type_info() {
 
$profile_types['custom'] = array(
   
'type' => 'custom',
   
'name' => t('Custome profile type'),
   
'addressfield' => FALSE,
  );
  return
$profile_types;
}

/**
 * Implements hook_commerce_checkout_pane_info_alter().
 */
function custom_commerce_commerce_checkout_pane_info_alter(&$checkout_panes) {
  global
$user;
 
$order = commerce_cart_order_load($user->uid);
 
$line_item = (!empty($order->commerce_line_items['und'][0]['line_item_id'])) ? commerce_line_item_load($order->commerce_line_items['und'][0]['line_item_id']) : NULL;
 
$product = (!empty($line_item->commerce_product['und'][0]['product_id'])) ? commerce_product_load($line_item->commerce_product['und'][0]['product_id']) : NULL;
  if (isset(
$checkout_panes['customer_profile_custom']) && isset($product->product_id) && $product->product_id == 1) {
   
$checkout_panes['customer_profile_custom']['enabled'] = FALSE;
  }
}
?>