You are here

function uc_wishlist_add_item in UC Wish List 6

Same name and namespace in other branches
  1. 8 uc_wishlist.module \uc_wishlist_add_item()
  2. 7 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
uc_wishlist_add_to_wishlist
uc_wishlist_user in ./uc_wishlist.module
Implementation of hook_user().

File

./uc_wishlist.module, line 659
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_create_wishlist();
    if (!$wid) {
      drupal_set_message(t('Could not create wish list. Adding item failed.'), 'error');
      return FALSE;
    }
    $created = TRUE;
  }
  $node = node_load($nid);
  if (is_null($data)) {
    $data = array(
      'module' => 'uc_product',
    );
  }
  $supported_node_types = array_merge(array_keys(uc_product_node_info()), uc_product_kit_product_types());
  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 = uc_wishlist_invoke_hook_add_to_cart($nid, $qty, $data);
  if (is_array($result) && !empty($result)) {
    foreach ($result as $row) {
      if ($row['success'] === FALSE && (!isset($row['silent']) || $row['silent'] === FALSE)) {
        $message = empty($row['message']) ? t('Sorry, that item is not available for purchase at this time.') : $row['message'];
        drupal_set_message($message, 'error');
        return;
      }
    }
  }
  $item = db_fetch_object(db_query("SELECT * FROM {uc_wishlist_products} WHERE wid = %d AND nid = %d AND data = '%s'", $wid, $nid, serialize($data)));

  // If the item isn't in the cart yet, add it.
  if (is_null($item) || $item === FALSE) {
    db_query("INSERT INTO {uc_wishlist_products} (wid, nid, qty, changed, data, purchase) VALUES (%d, %d, %d, %d, '%s', '')", $wid, $nid, $qty, time(), serialize($data));
    if ($msg) {
      drupal_set_message(t('<b>@product-title</b> added to <a href="!url">your wish list</a>.', array(
        '@product-title' => $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(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');
  }
}