Commerce Product add,delete,find,get line item id from product id, change quantity of a product in cart

Public

1. Add a product programmatically to cart.
2. Get line item id for a product in cart from product id.
3. Delete a product programmatically from cart
4. Alter product quantity in cart(add/remove).

</> CopyGet raw version
php
  1. /**
  2.  *
  3.  * function to check if a product exist in cart or not
  4.  * @param
  5.  * $product_id takes product id we are checking if exists or not as input.
  6.  * type take product id as integer or array of product ids
  7.  * @return
  8.  * return -1 if product does not exists
  9.  * returns -2 if cart if empty
  10.  * returns line item ids for the searched product(array of line item ids incase of multiple search of products else single line item id)
  11.  */
  12. function product_in_cart($product_id) {
  13. if (!is_array($product_id)) {
  14. global $user;
  15. $current_order = commerce_cart_order_load($user->uid);
  16. if (count($current_order->commerce_line_items) > 0) {
  17. $line_items = $current_order->commerce_line_items;
  18. foreach ($line_items['und'] as $key => $value) {
  19. $line_item = commerce_line_item_load($value['line_item_id']);
  20. $products = $line_item->commerce_product['und'];
  21. foreach ($products as $product_key => $product_value) {
  22. if ($product_id == $product_value['product_id']) {
  23. return $value['line_item_id'];
  24. }
  25. }
  26. }
  27. return -1;
  28. } else
  29. return -2;
  30. }
  31. else {
  32. global $user;
  33. $current_order = commerce_cart_order_load($user->uid);
  34. if (count($current_order->commerce_line_items) > 0) {
  35. $line_items = $current_order->commerce_line_items;
  36. foreach ($line_items['und'] as $key => $value) {
  37. $line_item = commerce_line_item_load($value['line_item_id']);
  38. $products = $line_item->commerce_product['und'][0]['product_id'];
  39. foreach ($product_id as $id) {
  40. if ($id == $products) {
  41. $line_item_ids[] = $value['line_item_id'];
  42. }
  43. }
  44. } if (isset($line_item_ids) >= 1) {
  45. return $line_item_ids;
  46. }
  47. else {
  48. return -1;
  49. }
  50. } else
  51. return -2;
  52. }
  53. }
  54.  
  55. /**
  56.  * function to delete a product from cart.
  57.  * @param
  58.  * $product_ids takes product id to be deleted as argument
  59.  * type it can be array of product ids or can be single product ids.
  60.  * @return
  61.  * returns -1 on failure.
  62.  * faliure is possible because of
  63.  * given product does not exist
  64.  * current cart object is empty
  65.  */
  66. function product_delete_cart($product_ids) {
  67. if (!is_array($product_ids)) {
  68. $line_item_id = product_in_cart($product_ids);
  69. if ($line_item_id != -1 && $line_item_id != -2) {
  70. global $user;
  71. $current_order = commerce_cart_order_load($user->uid);
  72. commerce_cart_order_product_line_item_delete($current_order, $line_item_id);
  73. } else
  74. return -1;
  75. }
  76. else {
  77. $line_item_ids = product_in_cart($product_ids);
  78. if ($line_item_ids != -1 && $line_item_ids != -2) {
  79. global $user;
  80. $current_order = commerce_cart_order_load($user->uid);
  81. foreach ($line_item_ids as $line_item_id) {
  82. commerce_cart_order_product_line_item_delete($current_order, $line_item_id);
  83. }
  84. }
  85. else {
  86. return -1;
  87. }
  88. }
  89. }
  90.  
  91. /**
  92.  * function to add a product to cart
  93.  * @param
  94.  * $product_id takes product id to be added to cart as argument
  95.  * @param
  96.  * $quantity to be added for the given product
  97.  * @param
  98.  * $uid user id
  99.  * this argument is optional,incase no value is passed for $uid, function takes the uid of the current user.
  100.  * @return
  101.  * returns line item id for the product added to cart
  102.  */
  103. function product_cart_add($product_id, $quantity, $uid = NULL) {
  104. $line_item = NULL;
  105. if ($uid == NULL) {
  106. global $user;
  107. $uid = $user->uid;
  108. }
  109. if ($product = commerce_product_load($product_id)) {
  110. $line_item = commerce_product_line_item_new($product, $quantity);
  111. $line_item = commerce_cart_product_add($uid, $line_item);
  112. }
  113. return $line_item;
  114. }
  115.  
  116. /**
  117.  * function to alter quantity of a product in the cart
  118.  * @param
  119.  * $product_id takes product id for which you need to alter the quantity in the cart
  120.  * @param
  121.  * $quantity takes the quantity to be removed or added to existing quantity of the product in the cart.
  122.  * @param
  123.  * $param takes remove/add as param to differenciate between adding of quantity or removing of quantity.
  124.  * @return
  125.  * returns -1 in case of failure.
  126.  */
  127.  
  128. function product_alter_quanity($product_id, $quantity, $param) {
  129. $line_item_id = product_in_cart($product_id);
  130. if ($line_item_id != -1 && $line_item_id != -2) {
  131. if ($param == 'remove') {
  132. $product_quantity_in_cart = commerce_line_items_quantity(array($line_item_id));
  133. if ($quantity >= $product_quantity_in_cart) {
  134. product_delete_cart($product_id);
  135. }
  136. else {
  137. $line_item = commerce_line_item_load($line_item_id);
  138. $line_item->quantity = $line_item->quantity - $quantity;
  139. commerce_line_item_save($line_item);
  140. }
  141. }
  142. elseif ($param == 'add') {
  143. //$line_item_id = product_in_cart($product_id);
  144. $line_item = commerce_line_item_load($line_item_id);
  145. $line_item->quantity = $line_item->quantity + $quantity;
  146. commerce_line_item_save($line_item);
  147. }
  148. }
  149. else {
  150. return -1;
  151. }
  152. }

Comments

drupaltonic's picture

Thanks man!!..that was really helpful.

Anonymous's picture

ditto - thanks much for these helpful snippets!

reubenlara's picture

I've added this code to a custom module, but can't seem to call the functions successfully or even find out how to pass a product ID to them? Would you mind explaining how to implement or pointing me to some resources that detail a step I'm missing? Thanks!