Created
September 20, 2011 21:00
-
-
Save rfay/1230314 to your computer and use it in GitHub Desktop.
Add to cart fule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function commerce_installments_rules_action_info() { | |
$actions['commerce_installments_action_add_to_cart'] = array( | |
'label' => t('Add a product to the cart'), | |
'group' => t('Commerce Installments'), | |
'parameter' => array( | |
'product' => array( | |
'type' => 'commerce_product', | |
'label' => t('Product'), | |
), | |
'line_item_type' => array( | |
'type' => 'text', | |
'label' => t('Line Item Type'), | |
'options' => 'commerce_installments_line_item_type_options' | |
), | |
), | |
); | |
return $actions; | |
} | |
/** | |
* Returns an array of line item names keyed by line item type, suitable for use | |
* with a select element #options. | |
*/ | |
function commerce_installments_line_item_type_options() { | |
$types = commerce_line_item_types(); | |
$options = array_intersect_key(commerce_line_item_type_get_name(), drupal_map_assoc($types)); | |
return $options; | |
} | |
/** | |
* Rules Action: Add a product to the cart. | |
* | |
* @param $product | |
* The product to add. | |
*/ | |
function commerce_installments_action_add_to_cart($product, $line_item_type) { | |
// Create the new product line item of the same type. | |
$line_item = commerce_product_line_item_new($product, 1, 0, array(), $line_item_type); | |
// Process the unit price through Rules so it reflects the user's actual | |
// purchase price. | |
rules_invoke_event('commerce_product_calculate_sell_price', $line_item); | |
// Only attempt an Add to Cart if the line item has a valid unit price. | |
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item); | |
if (!is_null($line_item_wrapper->commerce_unit_price->value())) { | |
// Add the product to the specified shopping cart. | |
$form_state['line_item'] = commerce_cart_product_add( | |
$form_state['values']['uid'], | |
$line_item, | |
isset($line_item->data['context']['add_to_cart_combine']) ? $line_item->data['context']['add_to_cart_combine'] : TRUE | |
); | |
drupal_set_message(t('%title added to <a href="!cart-url">your cart</a>.', array('%title' => $product->title, '!cart-url' => url('cart')))); | |
} | |
else { | |
drupal_set_message(t('%title could not be added to your cart.', array('%title' => $product->title)), 'error'); | |
} | |
} | |
/************* And here is the component in use ***************/ | |
/* | |
{ "rules_add_item_to_cart" : { | |
"LABEL" : "Add Item to Cart", | |
"PLUGIN" : "action set", | |
"REQUIRES" : [ "commerce_installments" ], | |
"USES VARIABLES" : { | |
"commerce_product" : { "label" : "Product", "type" : "commerce_product" }, | |
"line_item_type" : { "label" : "Line Item Type", "type" : "text" } | |
}, | |
"ACTION SET" : [ | |
{ "commerce_installments_action_add_to_cart" : { | |
"product" : [ "commerce-product" ], | |
"line_item_type" : [ "line_item_type" ] | |
} | |
} | |
] | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment