Function for programmatically add a product as line item by using product id
function product_cart_add($product_id, $quantity, $uid = NULL) {
$line_item = NULL;
if ($uid == NULL) {
global $user;
$uid = $user->uid;
}
if ($product = commerce_product_load($product_id)) {
$line_item = commerce_product_line_item_new($product, $quantity);
$line_item = commerce_cart_product_add($uid, $line_item);
}
return $line_item;
}
No comments:
Post a Comment