You are here

function uc_wishlist_form_alter in UC Wish List 6

Same name and namespace in other branches
  1. 8 uc_wishlist.module \uc_wishlist_form_alter()
  2. 7 uc_wishlist.module \uc_wishlist_form_alter()

Implementation of hook_form_alter().

Alter uc_product_add_to_cart_form instances to include 'add to wish list' button. Form submission is routed through uc_wishlist_add_to_wishlist to differentiate between 'add to cart' and 'add to wish list'. If this module is added to Ubercart core, then this can be simplified.

Alter uc_cart_checkout_form to include by default the delivery address of a wish list's owner, if items come from a single wish list.

File

./uc_wishlist.module, line 264
Allows users to create public shopping/wish lists.

Code

function uc_wishlist_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'uc_product') === 0 && strpos($form_id, '_add_to_cart_form_') !== FALSE) {
    global $user;

    //Checking if user has access to wish list
    if (user_access('access wish lists', $user)) {

      // Add the wish list button to the add to cart form.
      $form['wishlist'] = array(
        '#type' => 'submit',
        '#value' => t('Add to wish list'),
        '#submit' => array(
          'uc_wishlist_add_to_wishlist_submit',
        ),
      );
    }
  }
  if (strpos($form_id, 'uc_product_kit_add_to_cart_form_') === 0) {

    // When adding a product kit, disable its default submit handler.
    // Otherwise, it will also be added to the cart.
    $new_submit = $form['#submit'];
    $submit_position = array_search('uc_product_kit_add_to_cart_form_submit', $new_submit);
    if ($submit_position !== FALSE) {
      foreach ($form['#submit'] as $key => $function) {
        if ($key === $submit_position) {
          unset($new_submit[$key]);
        }
      }

      // Re-index array.
      $form['#submit'] = array_values($new_submit);
    }
  }
  if ($form_id == 'uc_cart_checkout_form' && !isset($_SESSION['cart_order'])) {
    $items = unserialize($form['cart_contents']['#value']);
    $wids = array();
    foreach ($items as $item) {
      if ($item->data['wid']) {
        $wids[] = $item->data['wid'];
      }
    }
    $wids = array_unique($wids);
    if (count($wids) > 1) {
      drupal_set_message(t('This order contains items from multiple wish lists. It is not possible to automatically address this order for its recipient.'));
    }
    else {
      if (count($wids) == 1) {
        $wishlist = uc_wishlist_load($wids[0]);
        if (!empty($wishlist->address->firstname) && !empty($wishlist->address->lastname) && !empty($wishlist->address->addr1) && !empty($wishlist->address->postcode)) {
          $form['panes']['delivery']['delivery_first_name']['#default_value'] = $wishlist->address->firstname;
          $form['panes']['delivery']['delivery_last_name']['#default_value'] = $wishlist->address->lastname;
          $form['panes']['delivery']['delivery_company']['#default_value'] = $wishlist->address->company;
          $form['panes']['delivery']['delivery_street1']['#default_value'] = $wishlist->address->addr1;
          $form['panes']['delivery']['delivery_street2']['#default_value'] = $wishlist->address->addr2;
          $form['panes']['delivery']['delivery_city']['#default_value'] = $wishlist->address->city;
          $form['panes']['delivery']['delivery_country']['#default_value'] = $wishlist->address->country;
          $form['panes']['delivery']['delivery_zone']['#default_value'] = $wishlist->address->zone;
          $form['panes']['delivery']['delivery_postal_code']['#default_value'] = $wishlist->address->postcode;
          $form['panes']['delivery']['delivery_phone']['#default_value'] = $wishlist->address->phone;
          drupal_set_message(t('This order contains items from a wish list. The delivery address has been automatically set to the preferred address from the wish list. You may change this address.'));
        }
      }
    }
  }
}