I have a form adding an item to the basket in the form of a table n x m where n and m are taxonomy terms of two different taxonomies ('type of the insert' and 'insert size').

In each cell the user can enter the number of items, with the n-th 'type of the insert' and m-th 'size'.

Form I create myself in the template-based taxonomy vocabularies. How can I add products to cart programmatically?

  • You'll need to state which ecommerce project you're using, Ubercart or Commerce
    – Shawn Conn
    Commented Apr 19, 2015 at 16:21

1 Answer 1


To add particular product id you have your product id and current user id you can easily do. Send the Product id and user id to this function this will add your product to the user cart.

This is the custom function used. The main part here is the commerce_product_line_item_new() and commerce_cart_product_add() this will add the product to that particular user cart.

function add_product_to_cart($pid,$uid=0){
    global $user;
    $uid = isset($uid) && !empty($uid) ? $uid : $user->uid;
    $line_item = commerce_product_line_item_new(commerce_product_load($pid));
    commerce_cart_product_add($uid, $line_item);

Hope this will help :)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.