Hi

is there a solution that i can select more tokens for the product registration title in the registration information pane?
At the moment i can only choose [title] and [index] as token for the registration title.

token for product registration title

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Adam Wood’s picture

Title: make it possible to choose more tokens for the product registration title » Add Token module support for the product registration title
Version: 7.x-2.0-beta5 » 7.x-2.x-dev
Status: Active » Needs review
FileSize
1.94 KB

We've had this requirement a few times, e.g. to add an product's date field data to the title for an event, and it's not that easy to override. Attached is a simple patch to add support for the Token module on the product registration title field, with the Commerce Order and Commerce Line Item objects available for each registration, whilst keeping the existing token functionality. You can get any product data you need from the Commerce Line Item.

It might be best to move all of the tokens for this into the Token module framework, and that would just require the module to define a custom token for the registration index. If a maintainer thinks this is a good idea, I'm happy to extend this patch. A decision would have to be made about what to do with the existing tokens.

Without using this patch, you need quite a messy form alter, as you have to re-use quite a bit of the original form to alter the elements. In case it's of interest to anyone, the following hook_form_FORM_ID_alter() will allow you to override the title without this patch (only tested on 7.x-2.x-dev):

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Overrides the Commerce Registration checkout pane form.
 *
 * @see commerce_registration_information_checkout_form()
 */
function MYMODULE_form_commerce_checkout_form_registration_alter(&$form, &$form_state, $form_id) {
  // We need to replicate all of this from the original form to be able to
  // override each line item that needs registration information.
  $order_id = $form_state['build_info']['args'][0]->order_number;
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order_id);
  $i = 0; // Main counter for all the registrations
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    if (!in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
      continue;
    }
    $id = (int) $line_item_wrapper->commerce_product->product_id->value();
    if (commerce_registration_product_has_registration_field($id)
      && registration_status('commerce_product', $id, TRUE) == 1) {
      $product = $line_item_wrapper->commerce_product;
      $line_item_id = $line_item_wrapper->line_item_id->value();
      $quantity = (int) $line_item_wrapper->quantity->value();
      $prodkey = $line_item_id . 'prod-' . $product->sku->value();
      for ($k = 0; $k < $quantity; $k++) {
        // We add a single registration for the quantity of the product being
        // purchased/registered for.
        $label = $i + 1;

        // Here you can alter the title for each registration. You have access
        // to the Commerce Order ($order_wrapper), Commerce Line Item
        // ($line_item_wrapper) and Commerce Product ($product) entities, as
        // well as the index ($label).
        $form['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['#title'] = t('Your custom title');
      }
    }
  }
}
blacklabel_tom’s picture

Hi Adam,

Thanks for your work on this issue, if you are happy to knock the module into shape with the token framework I would welcome any patches.

I think that would be the best way to fulfil this requirement.

Cheers

Tom

Adam Wood’s picture

Hi Tom,

I didn't notice that you were a maintainer. Small (Drupal) world...

Updated patch attached that provides support for tokens on the title and help text fields, and a custom token type for the module and token for the 'index'. It keeps support for the [title] and [index] tokens, but references to them have been removed on the form. Their tokens are now:

[title] -> [commerce-line-item:commerce-product:title]
[index] -> [commerce-registration:registration-index]

I'm not sure about some of the terminology used, mainly 'The numerical index of the registration for a product.', but take a look and see what you think.

Cheers

Adam

blacklabel_tom’s picture

Hi Adam,

It certainly is a small world!

Patch looks good to me, leaving as needs review for someone to OK this.

Give me a nudge if there is no movement on this in a couple of weeks and I'll push it into the dev branch. Hopefully someone will come along and review this in the meantime, but no problem if not.

Cheers

Tom

Adam Wood’s picture

Thanks Tom, will do.

There were a few inconsistencies in the hook_tokens() and a typo in the checkout_pane.inc that I've updated in the attached patch.

mabo1972’s picture

Status: Needs review » Reviewed & tested by the community

blacklabel_tom’s picture

Status: Reviewed & tested by the community » Closed (fixed)
blacklabel_tom’s picture

Sorry mabo1972, forgot to give you credit in the commit!