How can I add products to cart programmatically? 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?
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) &amp;&amp; !empty($uid) ? $uid : $user-&gt;uid;
    $line_item = commerce_product_line_item_new(commerce_product_load($pid));
    commerce_cart_product_add($uid, $line_item);
}


Hope this will help :)