You are here

function commerce_wishlist_user_has_product_in_wishlist in Commerce Wishlist 7.3

Returns whether or not a product is in a user's wish list.

This module looks to see if a product exists in any of the user's wishlists. It does this by loading all of the orders that are a user has and looping through the line items.

Parameters

int $product_id: The product ID.

int|null $uid: The UID of the user or NULL for the current user.

Return value

bool TRUE if the product exists, FALSE otherwise.

6 calls to commerce_wishlist_user_has_product_in_wishlist()
commerce_wishlist_add_form_validate in ./commerce_wishlist.module
Validate callback for commerce_cart_add_to_cart_form().
commerce_wishlist_form_alter in ./commerce_wishlist.module
Implements hook_form_alter().
commerce_wishlist_product_add in ./commerce_wishlist.module
Add a product to a wish list.
commerce_wishlist_product_add_ajax in ./commerce_wishlist.module
Page callback: Ajax product add.
commerce_wishlist_product_add_page in ./commerce_wishlist.module
Menu callback: Perform various actions (add to wishlist etc).

... See full list

File

./commerce_wishlist.module, line 1040
Provides a wish list for use in Drupal Commerce.

Code

function commerce_wishlist_user_has_product_in_wishlist($product_id, $uid = NULL, $wishlist = NULL) {
  if ($uid === NULL) {
    global $user;
    $uid = $user->uid;
  }
  if ($uid === 0) {
    return FALSE;
  }

  // Load all wish lists.
  $a = new EntityFieldQuery();
  $a
    ->entityCondition('entity_type', 'commerce_order', '=')
    ->propertyCondition('status', array(
    'wishlist',
  ), 'IN')
    ->propertyCondition('uid', $uid, '=');
  $results = $a
    ->execute();
  if (!isset($results['commerce_order'])) {
    return FALSE;
  }
  foreach (commerce_order_load_multiple(array_keys($results['commerce_order'])) as $order) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    foreach ($order_wrapper->commerce_line_items as $wrapper_line_item) {
      if (in_array('commerce_product', array_keys($wrapper_line_item
        ->getPropertyInfo()))) {
        if ($wrapper_line_item->commerce_product->product_id
          ->value() == $product_id) {
          return TRUE;
        }
      }
    }
  }
  return FALSE;
}