From 503a5a02699c5c73330575d9a3b9595eaf6b2a31 Mon Sep 17 00:00:00 2001
From: Shushu Inbar <shushu.i@gmail.com>
Date: Wed, 16 Jan 2013 01:13:28 +0200
Subject: [PATCH] Adding "Add a line item to the cart" action

---
 modules/cart/commerce_cart.rules.inc |   50 ++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/modules/cart/commerce_cart.rules.inc b/modules/cart/commerce_cart.rules.inc
index 9c0fdc2..d50ef8a 100644
--- a/modules/cart/commerce_cart.rules.inc
+++ b/modules/cart/commerce_cart.rules.inc
@@ -179,6 +179,38 @@ function commerce_cart_rules_action_info() {
     ),
   );
 
+  $actions['commerce_cart_line_item_add'] = array(
+    'label' => t('Add a line item to the cart'),
+    'parameter' => array(
+      'user' => array(
+        'type' => 'user',
+        'label' => t('User'),
+        'description' => t('Specify the user whose shopping cart order the product will be added to, typically <em>site:current-user</em>.'),
+      ),
+      'line_item' => array(
+        'type' => 'commerce_line_item',
+        'label' => t('Line item'),
+        'description' => t('Commerce line item entity to add to the cart.'),
+      ),
+      'combine' => array(
+        'type' => 'boolean',
+        'label' => t('Combine similar items in the cart'),
+        'description' => t('If checked, the line item will be combined added to an existing similar product line item in the cart by incrementing its quantity.'),
+        'default value' => TRUE,
+      ),
+    ),
+    'group' => t('Commerce Cart'),
+    'callbacks' => array(
+      'execute' => 'commerce_cart_rules_line_item_add',
+    ),
+    'provides' => array(
+      'product_add_line_item' => array(
+        'type' => 'commerce_line_item',
+        'label' => t('Added product line item'),
+      ),
+    ),
+  );
+
   return $actions;
 }
 
@@ -218,5 +250,23 @@ function commerce_cart_rules_product_add_by_sku($user, $sku, $quantity = 1, $com
 }
 
 /**
+ * Rules action: adds a line item to a user's shopping cart order.
+ */
+function commerce_cart_rules_line_item_add($user, $line_item, $combine = TRUE) {
+  // Pull the uid from the user passed in.
+  if (empty($user) || empty($user->uid)) {
+    $uid = NULL;
+  }
+  else {
+    $uid = $user->uid;
+  }
+
+  // Only return an added line item.
+  if ($new_line_item = commerce_cart_product_add($uid, $line_item, $combine)) {
+    return array('product_add_line_item' => $new_line_item);
+  }
+}
+
+/**
  * @}
  */
-- 
1.7.6.msysgit.0

