[Drupal 7 Commerce] Custom Module

Custom hooks written for a Drupal 7 based commerce site.

  • Adds extra customizations for the checkout process, such as disabling the 'back' button on Checkout pages and adjusting the login form options.
  • Converts the Add To Cart Form quantity widget from a text field to a drop-down list.
Completion Date
Commerce Site Customized Checkout
Platform(s)/Language(s)
Code Snippet
<?php
 
/**
 * Implements hook_form_alter()
 * Replicated from XXX project, by Eric.
 */
function commercesite_custom_form_alter(&$form, &$form_state, $form_id){
    //dpm($form);
    //dpm($form_state);
    //dpm($form_id);
    /**
     * Add extra customization for checkout process
     * and to change commerce's display of 'Title' to 'Product Name'
     */
    switch($form_id){
        case "commerce_product_ui_product_form":
            // Change 'Title' to 'Product Name'
            $form['title']['#title'] = 'Product Name';
        break;
        /* Uncomment to unset the 'back' button on Checkout pages
        case "commerce_checkout_form_checkout":
            unset($form['buttons']['back']);
        break;
        case "commerce_checkout_form_shipping":
            unset($form['buttons']['back']);
            $form['buttons']['continue']['#value']=t('Review Order Details');
        break;
        case "commerce_checkout_form_review":
            unset($form['buttons']['back']);
            $form['buttons']['continue']['#value']=t('Complete Order');
        break;
        */
        case "user_login":
            if (isset($form['have_pass'])){
              // Change the login form text and options
              $form['have_pass']['#title'] = 'Continue as Guest?';
              $form['have_pass']['#options'][0] = 'Yes';
              $form['have_pass']['#options'][1] = 'No';
              // Set the default value to the second radio button:
              $form['have_pass']['#default_value'] = 1;
              // Rearrage the order of fields:
              $form['have_pass']['#weight'] = -30;
              $form['name']['#weight'] = -20;
              $form['pass']['#weight'] = -10;
            }
            break;
 
        case "user_register_form":
            if (isset($form['actions']['continue_button'])){
                // Unset the "Checkout without an account" button from the
                // right-hand pane:
                unset($form['actions']['continue_button']);
            }
            break;
 
        case "commerce_checkout_form_review":
           $form['help']['#markup'] = '';
           $form['cart_contents']['#title'] = '<h2 class="review-order">Your Order</h2>';
            unset($form['checkout_review']['review']['#data']['account']);
            $form['customer_profile_shipping']['#title'] = 'Billing ';
 
            $sdata = $form['checkout_review']['review']['#data']['customer_profile_shipping']['data'];
            $stitle = $form['checkout_review']['review']['#data']['customer_profile_shipping']['title'];
            $form['checkout_review']['review']['#data']['customer_profile_shipping']['data'] = '<span class="title">'.$stitle.'</span>'.$sdata;
 
            $bdata = $form['checkout_review']['review']['#data']['customer_profile_billing']['data'];
            $btitle = $form['checkout_review']['review']['#data']['customer_profile_billing']['title'];
            $form['checkout_review']['review']['#data']['customer_profile_billing']['data'] = '<span class="title">'.$btitle.'</span>'.$bdata;
 
            unset($form['checkout_review']['review']['#data']['customer_profile_shipping']['title']);
            unset($form['checkout_review']['review']['#data']['customer_profile_billing']['title']);
 
            // Hide payment method select radio button from Review Order page
            hide($form['commerce_payment']['payment_method']);
 
            break;
 
        case "webform_client_form_45":
          honeypot_add_form_protection($form, $form_state, array('honeypot', 'time_restriction'));
          break;
 
    }
 
  /**
   *Convert the Add To Cart Form quantity widget from a text field to a drop-down list.
   * Added by Eric.  Source: https://www.drupal.org/node/482802
   */
    // Add to cart form from field api
  $str = 'commerce_cart_add_to_cart_form_';
  if (drupal_substr($form_id, 0, drupal_strlen($str)) == $str) {
    if (isset($form['quantity']) && $form['quantity']['#type'] == 'textfield') {
      $form['quantity']=array(
        '#type'=>'select',
        //'#title' => t('Quantity'),
        '#datatype' => 'integer',
        '#options' => array (
          0 => 'QTY.',
          1 => '1',
          2 => '2',
          3 => '3',
          4 => '4',
          5 => '5',
          6 => '6',
          7 => '7',
          8 => '8',
          9 => '9',
          10 => '10',
        ),
        '#default_value' => 0,
      );
 
    }
  }
  if ($form_id == 'views_form_commerce_cart_form_default') {
    //dpm($form_id);
    //dpm($form);
    foreach($form['edit_quantity'] as $key => $value) {
      $form['edit_quantity'][$key] = array(
        '#type'=>'select',
        '#datatype' => 'integer',
        '#options' => array (
          0 => 'QTY.',
          1 => '1',
          2 => '2',
          3 => '3',
          4 => '4',
          5 => '5',
          6 => '6',
          7 => '7',
          8 => '8',
          9 => '9',
          10 => '10',
        ),
        '#default_value' => $form['edit_quantity'][$key]['#default_value'],
        '#line_item_id' => $form['edit_quantity'][$key]['#line_item_id'],
      );
    }
  }
}
function commercesite_custom_user_view_alter(&$build) {
  if (array_key_exists('twitter', $build)) {
    unset($build['twitter']);
  }
}
function commercesite_custom_admin_paths_alter(&$paths){
    $paths['user/*/addressbook/*/create'] = false;
    $paths['user/*/addressbook/*/edit/*'] = false;
    $paths['user/*/addressbook/*/delete/*'] = false;
}