Skip to content

Instantly share code, notes, and snippets.

@esafwan
Created December 6, 2013 17:37
Show Gist options
  • Save esafwan/7828949 to your computer and use it in GitHub Desktop.
Save esafwan/7828949 to your computer and use it in GitHub Desktop.
Function to add product to cart programatically in drupal commerce.
<?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