Adding Products to a Customer's Cart via URL: Creating an Odoo Community Addon via Bitnami on Google Cloud
Quickly Add Powerful Functionality to Your Odoo ERP
Adding Products to a Customer's Cart via URL: Creating an Odoo Community Addon via Bitnami on Google Cloud
JD Berkowitz 16 May, 2023
Share this post

In today's digital era, eCommerce platforms have become a key component for businesses. One such platform is Odoo, an all-in-one business management software that includes CRM, eCommerce, billing, accounting, and much more. To make the most of Odoo, you can create addons to extend its functionalities. In this guide, we'll show you how to create an addon for Odoo 15 that allows you to add a product to a customer's cart by using a product ID and quantity in a URL.

This tutorial assumes you are:

  • Running Odoo Community
  • Familiar with Odoo Apps helps
  • Familiar with Odoo File Structure helps
  • Have the website module installed
  • Have product variants enabled

Github Repo

 
 


I. Introduction to Bitnami and Google Cloud

Bitnami is a library of installers or software packages for web applications that simplifies the process of installing and deploying these applications. Google Cloud Platform (GCP), on the other hand, is a suite of cloud computing services that offer a robust and scalable infrastructure for your applications. By using Bitnami on Google Cloud, we can easily deploy and manage Odoo Community. https://bitnami.com/stack/odoo

II. What is Odoo ERP?

Odoo is a free and open-source software suite that includes a variety of business applications such as CRM, accounting, inventory management, and project management. It is designed to help businesses of all sizes manage their operations more efficiently.

Odoo ERP is a cloud-based platform that can be accessed from anywhere. It is also highly customizable, so businesses can tailor it to their specific needs.

Some of the benefits of using Odoo ERP include:

  • Increased efficiency: Odoo ERP can help businesses automate many of their manual tasks, which can save time and money.

  • Improved accuracy: Odoo ERP can help businesses reduce errors by providing a single source of truth for data.

  • Better decision-making: Odoo ERP can provide businesses with insights into their data, which can help them make better decisions.

  • Increased collaboration: Odoo ERP can help businesses improve collaboration by providing a platform for employees to share information and work together on projects.

  • Enhanced security: Odoo ERP is a secure platform that is protected by industry-leading security measures.

  • Infinitely Extendable: You can customize Odoo to fit your unique business needs, whether it is process automation or ecommerce sales management, Odoo can be built to meet the demands of your business no matter the size.

III. Setting Up Odoo Demo on Google Cloud with Bitnami

To set up Odoo Community demo on Google Cloud using Bitnami, follow these steps:

1. Go to https://bitnami.com/stack/odoo

2. Click on 'Launch on Compute Engine' and follow the instructions to configure your instance.

3. After launching the instance, you'll have access to a fully functional and optimized Odoo installation with the current stable version.

Remember to configure the firewall rules to allow HTTP and HTTPS traffic to your instance.

IV. Understanding the Basics of Odoo Addon

An Odoo addon is a module that extends the system's functionality. It is made up of several files and directories, in this addon we will work with the absolute minimum amount of files which includes:

  • __init__.py
  • __manifest__.py
  • controllers/__init__.py
  • controllers/main.py

V. Creating the Addon Structure

Now, we'll create our addon structure. In your addons directory, create a new directory for your addon (e.g., `url_cart_addon`). Inside this directory, create these files and directories:

  • __init__.py
  • __manifest__.py
  • controllers/__init__.py
  • controllers/main.py

VI. Defining the Addon in the __manifest__.py File

The `__manifest__.py` file defines your addon. It includes information like the addon's name, version, dependencies, and more. Here is what it might look like for our addon:

{
    'name': 'URL Cart Addon',
    'version': '1.0',
    'depends': ['website_sale'],
    'data': [],
    'installable': True,
}

VII. Developing the Controller for the Addon

Odoo uses controllers to handle HTTP requests. In our controllers/main.py file, we'll write a function that adds a product to the cart.

from odoo import http
from odoo.http import request
from odoo.addons.website_sale.controllers.main import WebsiteSale
class AddByUrlController(WebsiteSale):
    @http.route(['/add_to_cart_by_url'], type='http', auth='public', website=True, csrf=False)
    def add_to_cart(self, product_id, quantity, **kwargs):
        # Parse product_id and quantity
        product_id = int(product_id)
        quantity = int(quantity)
        # Get the sale order (cart) or create a new one
        sale_order = request.website.sale_get_order(force_create=1)
        # Add the product to the cart
        sale_order._cart_update(
            product_id=product_id,
            add_qty=quantity,
        )
        return request.redirect('/shop/cart')

This function gets the current order or creates a new one, finds the product by its ID, adds the product to the cart, and redirects the user to the cart.

VIII. Creating the Route

Our `@http.route` decorator creates a route that adds a product to the cart. This route will be `/add_to_cart_by_url?product_id=1&quantity=2`, where `product_id` is the ID of the product variant and `quantity` is the quantity of the product to add or subtract (quantity can be negative). 

The `type='http'` parameter makes this route accessible through a HTTP request, `auth="public"` allows any visitor to access the route, and `website=True` ensures the route is part of the website, and `csrf=False` disables the cross site scripting protection, you can turn this on.

The __init__ files contain the python imports for the requsite modules. In controllers/__init__.py 

from . import main

In the __init__.py in the root folder contains the import for the controllers 

from . import controllers

IX. Testing the Addon

After developing the addon, the next step is to install and activate it in Odoo. To do this, follow these steps:

1. Restart your Odoo server.
On Bitnami on GCF you can SSH into your instance and run the command:

sudo /opt/bitnami/ctlscript.sh restart

2. Go to Settings and scroll to the bottom to enable Developer Tools. Then go to the apps menu and update the app list with the button now visible in the header menu.

3. Find your addon and click 'Install'.

Now, let's test the new route. In your browser, navigate to

https://your-instance.com/add_to_cart_by_url?product_id=1&quantity=2

Replace `<your-instance>`, `<product_id>`, and `<quantity>` with your Odoo URL, a product variant ID, and a quantity, respectively. You should see the product added to your cart.

You can find the product id in the Website Module by going to :

Website > Products > Product Variants 

When you select a product variant you will see the id parameter in the URL.

locate the product id in the odoo url

X. Conclusion

In this guide, we've shown you how to create an addon for Odoo 15 that adds a product to a customer's cart using a product ID and quantity in a URL. This is a powerful way to extend Odoo's functionality, helping you to create a more tailored eCommerce experience for your customers. Furthermore, by leveraging Bitnami and Google Cloud, we've seen how we can easily deploy and manage Odoo 15.

XI. References and Additional Resources

For further reading and learning about Odoo, Bitnami, and Google Cloud, check out these resources:

- Odoo's official documentation

- Bitnami documentation

- Google Cloud resources

In future blog posts, we'll continue exploring the world of Odoo addon development using Bitnami on Google Cloud. Stay tuned!

Sign in to leave a comment
Code & Coffee: A Step-by-Step Guide to Downloading Remote Files Using SFTP
Retrieve remote files from Odoo Community on Google Cloud