Solution is tested on odoo 10.
Follow these steps to add product to odoo shoping cart.
1. pass active order id to your external site by adding following lines to your main layout of odoo website page. (click customize html editor from top menu)
<t t-set="website_sale_order" t-value="website.sale_get_order()" />
<a t-if="website_sale_order" t-attf-href="http://yoursite/page.php?user_id={{ user_id.partner_id.id}}&order_id={{website_sale_order.id}}"> link to external site </a>
<a t-if="not website_sale_order" t-attf-href="http://yoursite/page.php?user_id={{ user_id.partner_id.id}}&order_id=null}"> link to external site </a>
Note: Use encoding as per your needs its a general idea of process flow.
2: From your external site insert data in sale.orderl to place a new order as (I am using odoo API in php)
$user_id = $_GET['user_id'];
$order_id = $_GET['order_id'];
$new_order_id = $models->execute_kw($db, $uid, $password,
'sale.order',
'create', // Function name
array( // Values-array
array( // First record
'name' =>$order_name,
'pricelist_id' => 1.00,
'partner_id' => $user_id,
'team_id' => 2, //crm_team having value website sales
'date_order' => $order_date->format('Y-m-d H:i:s'),
'state' => 'draft', // order is still in cart
'payment_term_id' => 1
)
)
);
3. update last so id in res.partner to set active order_id to cart.
if(is_int($new_order_id)){
$models->execute_kw($db, $uid, $password, 'res.partner', 'write',
array(array($user_id), array('last_website_so_id'=> $new_order_id)));
}
Note: skip step 2 and 3 if you get an valid order id from url. simply use $order_id in place of $new_order_id in next step
4. Insert items in the cart by inserting items in sale.order.line
$new_order_id = intval($order_id);
$order_line_id = $models->execute_kw($db, $uid, $password,
'sale.order.line',
'create', // Function name
array( // Values-array
array( // First record
'name' =>$order_name,
'company_id' => 1,
'price_unit' => $price,
'order_partner_id' => $user_id,
'order_id' => $new_order_id,
'customer_lead' => 1,
'product_id' => $product_id, // get product id from product.product (FK constraint)
'product_uom_qty' => $quantity,
'product_uom' => 1, //1 is for unit
'state' => 'draft' // status of order in cart
)
)
);
Note: set values according to your needs.
Hope it helps others. I spent many days to find the solution.