You are here

function uc_wishlist_add_item in UC Wish List 8

Same name and namespace in other branches
  1. 6 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.

1 call to uc_wishlist_add_item()
uc_wishlist_add_to_wishlist_submit in ./uc_wishlist.module
Submit handler of the uc_wishlist_add_to_wishlist.

File

./uc_wishlist.module, line 249
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);

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

  // $data = $node;
  // If Product Kit is enable, then add products into data array.
  $moduleHandler = \Drupal::service('module_handler');

  /*
    if ($moduleHandler->moduleExists('uc_product_kit') && $node->getType() == '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 ($moduleHandler->moduleExists('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 = $moduleHandler->invokeAll('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(\Drupal\Component\Utility\Xss::filter($message), 'error');
    return;
    }
    }
    }
  */
  $wishlist_manager = \Drupal::service('uc_wishlist.manager');
  $item = $wishlist_manager
    ->getWishlistItem($wid, $nid, $data);

  // If the item isn't in the cart yet, add it.
  if (is_null($item) || $item === FALSE) {
    $fields = [
      'wid',
      'nid',
      'qty',
      'changed',
      'data',
      'purchase',
    ];
    $values = [
      $wid,
      $nid,
      $qty,
      REQUEST_TIME,
      serialize($data),
      '',
    ];
    $id = $wishlist_manager
      ->createWishlistProduct($fields, $values);
    $productTitle = $node
      ->get('title')
      ->getValue()[0]['value'];

    // dpm($productTitle);
    if ($msg) {
      drupal_set_message(t('<b>@product-title</b> was added to <a href="@url">your wish list</a>.', [
        '@product-title' => $productTitle,
        '@url' => Url::fromRoute('uc_wishlist.wishlist')
          ->toString(),
      ]));
    }
  }
  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.', [
        '@url' => Url::fromRoute('uc_wishlist.wishlist')
          ->toString(),
      ]));
    }
  }
}