Skip to content

Instantly share code, notes, and snippets.

@m4tlch
Forked from esafwan/add_product.module
Last active August 29, 2015 14:18
Show Gist options
  • Save m4tlch/4a2ed94b102a00a9560d to your computer and use it in GitHub Desktop.
Save m4tlch/4a2ed94b102a00a9560d to your computer and use it in GitHub Desktop.
<?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