You are here

function commerce_wishlist_product_remove in Commerce Wishlist 7.3

Removes a product from a user's wish list.

If no wishlist is provided, it will grab the default wish list using commerce_wishlist_order_load(). If no account is provided, it will default to the logged in user.

Parameters

object $product: The product to remove.

int|null $account: The account of the owner of the wish list.

object|null $wishlist: The wishlist to remove the product from.

2 calls to commerce_wishlist_product_remove()
commerce_wishlist_action_remove_product_for_user in ./commerce_wishlist.rules.inc
Rules action callback to remove a specific product from user's wishlist.
commerce_wishlist_add_to_cart_remove in ./commerce_wishlist.module
Form callback: Removes a product from a wish list.
1 string reference to 'commerce_wishlist_product_remove'
commerce_wishlist_product_remove_line_item in ./commerce_wishlist.module
Removes a product from a user's wish list.

File

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

Code

function commerce_wishlist_product_remove($product, $account = NULL, $wishlist = NULL) {
  if ($account === NULL) {
    global $user;
    $account = $user;
  }
  if ($account->uid === 0) {
    return;
  }
  if ($wishlist === NULL) {

    // Get the default wish list.
    $wishlist = commerce_wishlist_order_load($account->uid);
  }
  $wishlist_wrapper = entity_metadata_wrapper('commerce_order', $wishlist);
  foreach ($wishlist_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    if ($line_item_wrapper->commerce_product->product_id
      ->value() === $product->product_id) {
      $line_item = $line_item_wrapper
        ->value();
      break;
    }

    // No item was found.
    return;
  }
  rules_invoke_all('commerce_wishlist_product_remove', $product, $account, $wishlist);

  // Remove the line item from the line item reference field.
  commerce_entity_reference_delete($wishlist, 'commerce_line_items', 'line_item_id', $line_item->line_item_id);
  commerce_line_item_delete($line_item->line_item_id);
  commerce_order_save($wishlist);
}