I would like ot be able to set a custom line item title to appear where ever the line item appears. I have taken a look at https://drupal.org/node/1029956 but I cannot see how to implement exactly what I am after. I need to be able to set a title for the line item that is not the product title, or has additional information to the product title. I have hacked commerce_product_line_item_title for now in order to get it to do what I need but I am sure there is a better way to do this.

TIA

function commerce_product_line_item_title($line_item) {
// Currently, just return the product's title. However, in the future replace
// this with the product preview build mode.
if ($product = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_product->value()) {
- return check_plain($product->title);
+ $alt_title = isset($line_item->data['newtitle']) ? $line_item->data['newtitle'] : '';
+ return check_plain($product->title) . $alt_title;
}
}

Comments

rszrama’s picture

Status: Active » Closed (fixed)

You should be able to use hook_commerce_line_item_type_info_alter() to set a different title callback function that includes your code.

kurtfoster’s picture

thx rszrama, much easier than I thought. I found the info for the function here hook_commerce_line_item_type_info_alter .

I had a read through the source on this page as well, commerce_line_item.api.php, to get more info on how to add the callback.

I can now add an item to the data array on my line item to either override or append to the product title. My code is below.

function mymodule_commerce_line_item_type_info_alter(&$line_item_types) {
  $line_item_types['product']['callbacks'] = array('title' => '_mymodule_commerce_product_line_item_title');
}

/**
* Returns an appropriate title for this line item for this site
*/
function _mymodule_commerce_product_line_item_title($line_item) {
 if ($product = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_product->value()) {
  if (isset($line_item->data['alt_title'])){
    return check_plain($line_item->data['alt_title']);
  }else{
    $alt_title = isset($line_item->data['svbtitle']) ? $line_item->data['svbtitle'] : '';
    return check_plain($product->title) . $alt_title;
  }
 }
}
rszrama’s picture

Great, glad you got it working. Just make sure that that svbtitle has been sanitized before printing it out like this! : )

johnhanley’s picture

Issue summary: View changes

Thanks @kurtfoster. Your code example of how to override a line item title on the order edit page saved me time (even 4+ years later).

rszrama’s picture

That's awesome, John! : D