This question has been flagged

I have a custom module with a form. Based on the answers inside this form I’m generating order line. After user sends this form I’m creating sale order with all products from the generated order line.

So from JavaScript I’m sending an JSON with products to buy:

order_data = [{product_id: 1, amount: 10, …},{product_id: 2, …}, …];

note = ‘’;

this._rpc({

        route: '/api/create_order',

        params: { order_products: order_datanote: note }

    }).then((data=> {

        window.location = '/contactus-thank-you';

    }).catch((error=> {

        console.error(error);

    });

 

And then inside Python I’m creating sale order based on the JSON:

@http.route('/api/create_order'type='json'auth='user'website=True)

def create_order(self, **kw):

    uid = http.request.env.context.get('uid')

    partner_id = http.request.env['res.users'].search([('id','=',uid)]).partner_id.id

    

    order_products = kw.get('order_products', [])

    note = kw.get('note''')

    order_line = []

 

    for product in order_products:

 

        amount = 0

        if 'custom_amount' in product:

            amount = product['custom_amount']

        else:

            amount = product['amount']

 

        if amount > 0:

            order_line.append(

                (00, {

                    'product_id': product['product_id'],

                    'product_uom_qty': amount,

                }))

 

    order_data = {

        'name': http.request.env['ir.sequence'].with_user(SUPERUSER_ID).next_by_code('sale.order'or _('New'),

        'partner_id': partner_id,

        'order_line': order_line,

        'note': note,

    }

 

    result_insert_record = http.request.env['sale.order'].with_user(SUPERUSER_ID).create(order_data)

    return result_insert_record.id

 

But instead of generating sale order directly I need to use workflow from Odoo’s eCommerce addon. That way user can edit for example delivery address, choose payment etc. So I think I just need to put all the product inside a cart programmatically and then rest will be taken care of by Odoo built-in functionality.

But how? I’ve tried to find something inside Odoo’s source code but it is quite hard to grasp anything.

Avatar
Discard
Best Answer

Hi Andrzej:

The eCommerce module leverages the same models that are used by the regular Sales app i.e. sale.order, sale.order.line, etc. It creates a Quotation in the backend (with the Website tagged onto the Order) as soon as the user gets into checkout mode.

TIP: You can check this out by adding items to the shopping cart, getting into checkout mode and then checking the list of Quotations/Orders in the backend by removing the default filter criteria.

Avatar
Discard
Author

I tried setting 'website_id' in the sale order but user does not see anything inside their cart. It must be something more to it.

You're right. This needs more research. I think what you are attempting to do is to get into the workflow before the quotation is created.

Hi, Was this ever solved?

Author

Great find.

Since I couldn't add a follow-up comment in stackoverflow, I'm placing it here:

I'm having a hard time trying to put all the pieces together to establish the following:

I've modified the website_sale.products_item template to display just the products and replaced product_price with a One2many field that I've added to the product_template model (inherited) which displays the allowed quantity available for each product.

Looking for a way to then take those quantities per product, pass them to a javascript that will determine if the selected quantities for each product exist in an allowed combination table.

My ultimate goal is to have the client select the allowed amount per product(all on the same page), click a button that will add the items to the cart (and correct quantities) in one step and take them to the cart, then finish off with the regular process.

I'm not too keen on the process of executing the javascript part from within a button in xml, and stitching the /api/create_order controller route to the sale_get_order() script above..

Any guidance in the right direction is truly appreciated.

Author

It is a little bit hard to tell without any code sample.

But I would avoid using JavaScript for handling logic if possible. Especially if those limits must be enforced (customer can easily modify JS).

I would write a custom controller that you would call from JavaScript (when button is clicked) - similar to a script from my original question.

Controller then would take care of the logic and checking if given values are correct. Next it would call sale_get_order() to get or create new order. Then you could add order lines to the order generated by sale_get_order. Lastly controller can redirect customer to a cart webpage.