You are here

function uc_wishlist_add_item in UC Wish List 7

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

Adds an item to a user's wish list.

2 calls to uc_wishlist_add_item()
uc_wishlist_add_to_wishlist_submit in ./uc_wishlist.module
Submit handler of the uc_wishlist_add_to_wishlist.
uc_wishlist_user_login in ./uc_wishlist.module
Implements hook_user_login().

File

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

Code

function uc_wishlist_add_item($nid, $qty = 1, $data = NULL, $wid = NULL, $msg = TRUE, $check_redirect = TRUE) {
  $wid = $wid ? $wid : uc_wishlist_get_wid();
  $created = FALSE;
  if (!$wid || $wid === NULL) {
    $wid = uc_wishlist_save_wishlist();
    if (!$wid) {
      drupal_set_message(t('Could not create wish list. Adding item failed.'), 'error');
      return FALSE;
    }
    $created = TRUE;
  }
  $node = node_load($nid);

  // Adding data variables.
  if (empty($data)) {
    $data = array(
      'module' => 'uc_product',
    );
  }
  elseif (!array_key_exists('module', $data)) {
    $data['module'] = 'uc_product';
  }

  // If Product Kit is enable, then add products into data array.
  if (module_exists('uc_product_kit') && $node->type == 'product_kit') {

    // Adding products of the product kit into data object.
    $products = $node->products;
    foreach ($products as $pid => $product) {
      $data['products'][$pid]['nid'] = $pid;
      $data['products'][$pid]['qty'] = $product->qty;

      // Creating attributes array.
      $attributes = array();

      // Getting attributes list of the product.
      $product_attributes = $product->attributes;

      // Iterating through the attributes list.
      foreach ($product_attributes as $aid => $product_attribute) {
        $attribute[$product_attribute->aid] = $product_attribute->default_option;
      }

      // Assigning attributes to data array.
      $data['products'][$pid]['attributes'] = $attributes;
    }
  }

  // If product kit module is install in the site, then we need to check if the
  // product is product kit or product.
  if (module_exists('uc_product_kit')) {
    $supported_node_types = array_merge(array_keys(uc_product_node_info()), array_keys(uc_product_kit_node_info()));
  }
  else {
    $supported_node_types = array_keys(uc_product_node_info());
  }

  // Checking if the node is product type or product kit type.
  if (!in_array($node->type, $supported_node_types)) {
    drupal_set_message(t('@title is not a product. Unable to add to wish list.', array(
      '@title' => $node->title,
    )), 'error');
    return;
  }
  $result = module_invoke_all('add_to_cart', $nid, $qty, $data);
  if (is_array($result) && !empty($result)) {
    foreach ($result as $row) {
      if ($row['success'] === FALSE) {
        if (isset($row['message']) && !empty($row['message'])) {
          $message = $row['message'];
        }
        else {
          $message = t('Sorry, that item is not available for purchase at this time.');
        }
        drupal_set_message(filter_xss($message), 'error');
        return;
      }
    }
  }
  $result = db_query("SELECT * FROM {uc_wishlist_products} WHERE wid = :wid AND nid = :nid AND data = :data", array(
    ':wid' => $wid,
    ':nid' => $nid,
    ':data' => serialize($data),
  ));
  $item = $result
    ->fetchObject();

  // If the item isn't in the cart yet, add it.
  if (is_null($item) || $item === FALSE) {
    $id = db_insert('uc_wishlist_products')
      ->fields(array(
      'wid' => $wid,
      'nid' => $nid,
      'qty' => $qty,
      'changed' => REQUEST_TIME,
      'data' => serialize($data),
      'purchase' => '',
    ))
      ->execute();
    if ($msg) {
      drupal_set_message(t('<b>@product-title</b> added to <a href="!url">your wish list</a>.', array(
        '@product-title' => filter_xss($node->title),
        '!url' => url('wishlist'),
      )));
    }
  }
  else {

    // Update the item instead.
    $qty += $item->qty;
    $wpid = $item->wpid;
    uc_product_update_wishlist_item($nid, $data, min($qty, 999999), $wid, $wpid);
    if ($msg) {
      drupal_set_message(filter_xss(t('Your <a href="!url">wish list</a> has been updated.', array(
        '!url' => url('wishlist'),
      ))));
    }
  }
  if ($check_redirect) {
    if (isset($_GET['destination'])) {
      drupal_goto();
    }
  }
  if ($created) {
    drupal_goto('wishlist');
  }
}