You are here

function commerce_wishlist_product_added_to_cart in Commerce Wishlist 7.2

Same name and namespace in other branches
  1. 7.3 commerce_wishlist.module \commerce_wishlist_product_added_to_cart()

Form submit handler called after a wishlist product has been added to the cart.

1 string reference to 'commerce_wishlist_product_added_to_cart'
commerce_wishlist_form_alter in ./commerce_wishlist.module
Implements hook_form_alter().

File

./commerce_wishlist.module, line 391
Provides the wishlist for use in Drupal Commerce.

Code

function commerce_wishlist_product_added_to_cart($form, &$form_state) {

  // Load the line item, add wishlist user ID in additional data, and then save
  // back the changes.
  $line_item = commerce_line_item_load($form_state['line_item']->line_item_id);
  $line_item->data['wishlist_products'][] = array(
    'node_id' => $form_state['values']['wishlist_nid'],
    'user_id' => $form_state['values']['wishlist_uid'],
    'product_id' => $form_state['values']['product_id'],
  );
  commerce_line_item_save($line_item);

  // Get the target node ID and other information necessary for Rules event.
  $query = db_select('commerce_wishlist', 'cw')
    ->addTag('commerce_wishlist')
    ->fields('wi', array(
    'nid',
  ))
    ->condition('cw.uid', $form_state['values']['wishlist_uid'])
    ->condition('wi.product_id', $form_state['values']['product_id']);
  $query
    ->join('commerce_wishlist_item', 'wi', 'wi.wishlist_id = cw.wishlist_id');
  $nid = $query
    ->execute()
    ->fetchField();
  $account = user_load($form_state['values']['wishlist_uid']);
  $product = commerce_product_load($form_state['values']['product_id']);
  $node = node_load($nid);
  rules_invoke_event('commerce_wishlist_event_product_added_to_cart', $account, $product, $node);
}