Customize Line Item Title in Drupal Commerce

Public

Register custom title callback at first, you can add title callback per line item type, in the example we add the callback to product line item:

</> CopyGet raw version
php
  1. /**
  2.  * Implements commerce_line_item_type_info_alter().
  3.  */
  4. function MODULE_NAME_commerce_line_item_type_info_alter(&$line_item_types) {
  5. $line_item_types['product']['callbacks'] = array('title' => '_MODULE_NAME_product_line_item_title');
  6. }

Custom title callback, you can use any logic here.

</> CopyGet raw version
php
  1. /**
  2.  * Tweaks line item title.
  3.  */
  4. function _MODULE_NAME_product_line_item_title($line_item) {
  5. $product = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_product->value();
  6. if ($product) {
  7. // Put your custom logic here.
  8. $product->title = $product->title . ' custom suffix';
  9. return check_plain($product->title);
  10. }
  11. }

Comments

Wael's picture

Hi,
Cool, but it does not work. Changes made at commerce_line_item_type_info_alter are not persisting. does any body have any idea about how to fix?