In Drupal Commerce, which hook do I use to alter product entity data in a product display?** And it needs to be a hook that fires each time I change an attribute that causes a different product entity to be loaded in the display.

I added a line item to my product type called "Comments". It is a textarea, and it has a field description too. Then, on my product type, I added the following fields:

  • Customizable? (On/Off checkbox)

  • Custom Field Label

  • Custom Field Description

As each product entity is about to be displayed (whether it's the first one shown when the page loads, or I make a drop down selection to change which one shows), I want to check if that product entity is "Customizable". If it is, I want to rename the title of the "Comments" line item with the value stored in "Custom Field Label". And I want to change the Comments field description with the value stored in "Custom Field Description". If the product entity is NOT "Customizable", then I want to hide the field completely (such as by converting the field type from a textarea to a hidden field). This way, the "Comments" line item really only shows for the product entities we want it to, and it can have a custom label and instruction when it does show.

Any help someone can give is greatly appreciated. I am having a lot of trouble even figuring out which hook fires each time a product entity is loaded, either the first time, or when switched via ajax.

hook_entity_load may be the answer that some folks are looking for in situations that differ from mine.

For my specific need, I discovered that everything I needed to accomplish could be done with hook_form_alter. The form_state holds the value of the currently selected SKU in $form_state['default_product']->sku. Here is a hook_form_alter function I wrote to accomplish my goals:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    if(strstr($form_id, 'commerce_cart_add_to_cart_form_')) {

        // Load the current product entity
        $prod = commerce_product_load_by_sku($form_state['default_product']->sku);

        // Check and see if the produt is customizeable or not, and then react
        $customizeable = $prod->field_prod_customizable['und'][0]['value'];
        if(isset($customizeable) && $customizeable == 1) {
            // Grab the custom label and instruction
            $customize_label = $prod->field_prod_customize_label['und'][0]['value'];
            $customize_instruction = $prod->field_prod_customize_instruction['und'][0]['value'];

            // Alter the field to display the custom label and instruction
            $form['line_item_fields']['field_prod_comments']['und'][0]['value']['#title'] = $customize_label;
            $form['line_item_fields']['field_prod_comments']['und'][0]['value']['#description'] = $customize_instruction;
        }
        else {
            // If not a customizeable product, then make the field hidden and not required
            $form['line_item_fields']['field_prod_comments']['und'][0]['value']['#type'] = 'hidden';
            $form['line_item_fields']['field_prod_comments']['und'][0]['value']['#required'] = 0;
        }
    }
}

Note that the textarea field I added to my Product line item type is called field_prod_comments. The fields I added to my product entity are field_prod_customizable, field_prod_customize_label, and field_prod_customize_instruction.

  • Checkout entity_metadata_wrapper for working with commerce entities. Also, note, $product is already loaded in $form_state['default_product'] – David Thomas Feb 3 '14 at 22:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.