Skip to Content
Odoo Menu
  • Log ind
  • Prøv gratis
  • apps
    Økonomi
    • Bogføring
    • Fakturering
    • Udgifter
    • Regneark (BI)
    • Dokumenter
    • e-Signatur
    Salg
    • CRM
    • Salg
    • POS Butik
    • POS Restaurant
    • Abonnementer
    • Udlejning
    Hjemmeside
    • Hjemmesidebygger
    • e-Handel
    • Blog
    • Forum
    • LiveChat
    • e-Læring
    Forsyningskæde
    • Lagerbeholdning
    • Produktion
    • PLM
    • Indkøb
    • Vedligeholdelse
    • Kvalitet
    HR
    • Medarbejdere
    • Rekruttering
    • Fravær
    • Medarbejdersamtaler
    • Anbefalinger
    • Flåde
    Marketing
    • Markedsføring på sociale medier
    • E-mailmarketing
    • SMS-marketing
    • Arrangementer
    • Automatiseret marketing
    • Spørgeundersøgelser
    Tjenester
    • Projekt
    • Timesedler
    • Udkørende Service
    • Kundeservice
    • Planlægning
    • Aftaler
    Produktivitet
    • Dialog
    • Godkendelser
    • IoT
    • VoIP
    • Vidensdeling
    • WhatsApp
    Tredjepartsapps Odoo Studio Odoo Cloud-platform
  • Brancher
    Detailhandel
    • Boghandel
    • Tøjforretning
    • Møbelforretning
    • Dagligvarebutik
    • Byggemarked
    • Legetøjsforretning
    Mad og værtsskab
    • Bar og pub
    • Restaurant
    • Fastfood
    • Gæstehus
    • Drikkevareforhandler
    • Hotel
    Ejendom
    • Ejendomsmægler
    • Arkitektfirma
    • Byggeri
    • Ejendomsadministration
    • Havearbejde
    • Boligejerforening
    Rådgivning
    • Regnskabsfirma
    • Odoo-partner
    • Marketingbureau
    • Advokatfirma
    • Rekruttering
    • Audit & certificering
    Produktion
    • Tekstil
    • Metal
    • Møbler
    • Fødevareproduktion
    • Bryggeri
    • Firmagave
    Heldbred & Fitness
    • Sportsklub
    • Optiker
    • Fitnesscenter
    • Kosmetolog
    • Apotek
    • Frisør
    Håndværk
    • Handyman
    • IT-hardware og support
    • Solenergisystemer
    • Skomager
    • Rengøringsservicer
    • VVS- og ventilationsservice
    Andet
    • Nonprofitorganisation
    • Miljøagentur
    • Udlejning af billboards
    • Fotografi
    • Cykeludlejning
    • Softwareforhandler
    Gennemse alle brancher
  • Community
    Få mere at vide
    • Tutorials
    • Dokumentation
    • Certificeringer
    • Oplæring
    • Blog
    • Podcast
    Bliv klogere
    • Udannelselsesprogram
    • Scale Up!-virksomhedsspillet
    • Besøg Odoo
    Få softwaren
    • Download
    • Sammenlign versioner
    • Udgaver
    Samarbejde
    • Github
    • Forum
    • Arrangementer
    • Oversættelser
    • Bliv partner
    • Tjenester til partnere
    • Registrér dit regnskabsfirma
    Modtag tjenester
    • Find en partner
    • Find en bogholder
    • Kontakt en rådgiver
    • Implementeringstjenester
    • Kundereferencer
    • Support
    • Opgraderinger
    Github Youtube Twitter LinkedIn Instagram Facebook Spotify
    +1 (650) 691-3277
    Få en demo
  • Prissætning
  • Hjælp

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Bogføring
  • Lager
  • PoS
  • Projekt
  • MRP
All apps
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Hjælp

Programmatically add products to a cart – Odoo 13

Tilmeld

Få besked, når der er aktivitet på dette indlæg

Dette spørgsmål er blevet anmeldt
javascriptsalesale.orderecommercewebsite
1 Svar
9761 Visninger
Avatar
Marcus

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_data, note: 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(

                (0, 0, {

                    '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.

1
Avatar
Kassér
Avatar
Paresh Wagh
Bedste svar

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.

0
Avatar
Kassér
Marcus
Forfatter

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.

Paresh Wagh

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.

Mike Cordero

Hi, Was this ever solved?

Marcus
Forfatter

Hi Mike, yes I did finally solve this. Check my thread on stackoverflow

https://stackoverflow.com/questions/63034005/programmatically-add-products-to-a-cart-odoo-13/63244596#63244596

Mike Cordero

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.

Marcus
Forfatter

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.

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Tilmeld dig
Related Posts Besvarelser Visninger Aktivitet
Javascript error Odoo 13 e-commerce/website
javascript ecommerce website 13
Avatar
Avatar
1
nov. 19
5520
Website Information
sale ecommerce website v9
Avatar
0
feb. 17
3815
create a Button on the home page of website
javascript ecommerce website website_sale OWL
Avatar
0
aug. 24
2260
Pass data between controller and js
javascript ecommerce controller website eCommerce
Avatar
Avatar
1
feb. 22
3686
How to submit an element of an radio list in website? Javascript event problem.
javascript ecommerce website website_sale odoo12
Avatar
0
feb. 21
3368
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Oversættelser
Tjenester
  • Odoo.sh-hosting
  • Support
  • Opgradere
  • Individuelt tilpasset udvikling
  • Uddannelse
  • Find en bogholder
  • Find en partner
  • Bliv partner
Om os
  • Vores virksomhed
  • Brandaktiver
  • Kontakt os
  • Stillinger
  • Arrangementer
  • Podcast
  • Blog
  • Kunder
  • Juridiske dokumenter • Privatlivspolitik
  • Sikkerhedspolitik
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo er en samling open source-forretningsapps, der dækker alle dine virksomhedsbehov – lige fra CRM, e-handel og bogføring til lagerstyring, POS, projektledelse og meget mere.

Det unikke ved Odoo er, at systemet både er brugervenligt og fuldt integreret.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now