This question has been flagged
2 Replies
10687 Views

I want to connect a external website with the Odoo shopping cart. The site is done using Odoo web service API, now I want to connect it to Odoo eCommerce.

Looking at the traffic when a product is added I see the a HTTP POST to /shop/cart/update with data like:

csrf_token=XXX&product_id=99&add_qty=1

I also see a cookie with a session_id.

So my question is how to make this cookie and the csrf_token to permit adding products from outside the shopping cart.

Avatar
Discard
Author Best Answer

Thanks for your extensive answer, it looks quite promising!

But I have a problem with the workflow: I have an external website where my products from Odoo are shown with a nice design, search engine, etc. and after selecting a product I want to send the user to my Odoo shopping cart with this product already added. So in this case there doesn't exist a active order id yet, it would have to be created.

It's like simulating a user coming to Odoo shop clicking on "Add to cart" on a product page.

Avatar
Discard
Author

Finally I managed to add a product by simply simulating a click on "Add to cart" button on product page using Javascript:

<pre>

document.getElementById("add_to_cart").click();

</pre>

Best Answer

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}}&amp;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}}&amp;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.

Avatar
Discard