Created
December 6, 2013 17:37
-
-
Save esafwan/7828949 to your computer and use it in GitHub Desktop.
Function to add product to cart programatically in drupal commerce.
This file contains 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
<?php | |
function my_module_product_add($pid,$quantity=1){ | |
if ($product = commerce_product_load($pid) ) { | |
global $user; | |
//uid of the logged in user | |
$uid = $user->uid; | |
$line_item = commerce_product_line_item_new($product, $quantity); | |
$line_item->field_ticket_type['und'][]['value'] = 0; | |
$line_item = commerce_cart_product_add($uid, $line_item, FALSE); | |
//optionally set message if you want | |
drupal_set_message("Done! You just did it."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment