How do I override a function from a module? Firstly, sorry if this answer is covered elsewhere. I've done a lot of searching and can only find info on overring theme functions and hooks.

I am using a module that builds a price table for Drupal Commerce items. There is a function that formats the table headers:

/**
 * Helper function that takes care of the quantity displayed in the headers of 
 * the price table.
 */
function commerce_price_table_display_quantity_headers($item) {
  // Set the quantity text to unlimited if it's -1.
  $max_qty = $item['max_qty'] == -1 ? t('Unlimited') : $item['max_qty'];
  // If max and min qtys are the same, only show one.
  if ($item['min_qty'] == $max_qty) {
    $quantity_text = $item['min_qty'];
  }
  else {
    $quantity_text = $item['min_qty'] . ' - ' . $max_qty;
  }
  return $quantity_text;
}


As you can see, this is not a theme function where I can override it in template.php but I can to tweak some of the output.

Obviously I don't want to edit the module itself in case it gets updated in the future, so, how can I redefine this function so I can chop and change a few things?

My work so far...

So far, I have tried to create it as a seperate module with a few subtle changes to show if it's working or not, but it's not overriding any of the output.

Info file

; $id$
name = Price Table: Tweaked Display
description = A different layout for the price table as shown on the product display nodes
package = Commerce (contrib)
core = 7.x

dependencies[] = commerce_product
dependencies[] = commerce_price
dependencies[] = commerce_price_table


Module File



 /**
 * Override of the helper function that takes care of the quantity displayed in the headers of 
 * the price table.
 */
function commerce_table_tweak_display_quantity_headers($item) {
  // Set the quantity text to unlimited if it's -1.
  $max_qty = $item['max_qty'] == -1 ? t('Unlimited gnhh') : $item['max_qty'];
  // If max and min qtys are the same, only show one.
  if ($item['min_qty'] == $max_qty) {
    $quantity_text = $item['min_qty'];
  }
  else {
    $quantity_text = $item['min_qty'] . ' - this is working - ' . $max_qty;
  }
  return $quantity_text;
}
It's Drupal...there's always a way, but the amount of time it will take to do might make you think twice :)

If you look a bit further up the food chain so-to-speak you'll see that this function is exclusively used by commerce_price_table_field_formatter_view(), which declares a field formatter that is used for the commerce_price_table field type.

With that in mind, you can quite easily implement your own field formatter, assign it to the commerce_price_table field type, and use as much custom code as you want, all the time keeping in line with best practices.

Basically you need to implement hook_field_formatter_info():

function MYMODULE_field_formatter_info() {
  return array(
    'MYMODULE_commerce_multiprice_default' =&gt; array(
      'label' =&gt; t('MyModule Price chart'),
      'field types' =&gt; array('commerce_price_table'),
      'settings' =&gt; array(
        'calculation' =&gt; FALSE,
        'price_label' =&gt; t('Price'),
        'quantity_label' =&gt; t('Quantity'),
        'table_orientation' =&gt; t('Orientation'),
      ),
    ),
  );
}


And then implement hook_field_formatter_view(), field_formatter_settings_form() and (optionally) hook_field_formatter_summary(). 

For each of these functions just take the code from the same function in the contrib module, and make the changes where you need to.