Enable Dark Mode!
how-to-add-the-catalog-feature-in-odoo-18.jpg
By: Javid Ahammed

How to Add the Catalog Feature in Odoo 18

Technical

Odoo 18 enhances product catalog functionality, allowing users to efficiently browse, filter, and select products within the system. This feature simplifies product selection by enabling users to choose product variations, apply category filters, and manage product attributes directly from the catalog interface.

For example, in a sales order, users can seamlessly select products from the catalog, where items can be sorted and filtered for quick access.

How to Add the Catalog Feature in Odoo 18-cybrosys

The catalog view displays products in a Kanban format, featuring an "Add" button to include items in the sales order line and a "Remove" button to remove them. Additionally, a "Back to Order" button allows users to quickly return to the sales order form, improving workflow efficiency. The catalog feature provides an intuitive way to manage product selection within sales orders.

How to Add the Catalog Feature in Odoo 18-cybrosys

In this blog, we will walk through the steps to add a catalog feature to a custom module in Odoo 18. We will start by defining the necessary models and fields, followed by creating the corresponding views.

Step 1: Inherit ‘product.catalog.mixin’ for Catalog Functionality

To integrate catalog functionalities into a custom model, inherit the product.catalog.mixin. This mixin enables catalog selection within the model.

class WarrantyRequest(models.Model):
   _name = 'warranty.request'
   _description = 'Warranty Request'
   _inherit = ['mail.thread', 'mail.activity.mixin', 'product.catalog.mixin']

Step 2: Add the Catalog Button in XML

To allow users to access the catalog from a one2many list view, add a button named action_add_from_catalog inside the <control> tag. This button will open the catalog selection interface when clicked.

<button name="action_add_from_catalog" type="object"
       string="Catalog" class="px-4 btn-link"
       context="{'order_id': parent.id}"/>

Final XML Code:

<notebook>
   <page id="product" name="Product">
       <field name="warranty_line_ids" mode="list"
              widget="section_and_note_one2many"
              context="{'default_display_type': False}">
           <list editable='bottom'>
               <control>
                   <create name="add_product_control" string="Add a product"/>
                   <create name="add_section_control"
                           string="Add a section"
                           context="{'default_display_type': 'line_section'}"/>
                   <create name="add_note_control"
                           string="Add a note"
                           context="{'default_display_type': 'line_note'}"/>
                   <button name="action_add_from_catalog" type="object"
                           string="Catalog" class="px-4 btn-link"
                           context="{'order_id': parent.id}"/>
               </control>
               <field name="product_id" required="not display_type"/>
               <field name="name"/>
               <field name="display_type" column_invisible="1"/>
               <field name="quantity" required="not display_type"/>
               <field name="uom_id"/>
               <field name="lst_price"/>
               <field name="warranty_charge" sum="Warranty Charge"/>
           </list>
       </field>
   </page>
</notebook>

This implementation ensures that the "Catalog" button appears in the control panel of the product list view, enabling seamless product selection from the catalog.

Step 3: Define the action_add_from_catalog Function in the Child Model

In the warranty.request.line model, define the action_add_from_catalog method. This function retrieves the parent warranty.request record using the context variable order_id and triggers its catalog action.

def action_add_from_catalog(self):
   warranty_request = self.env['warranty.request'].browse(
       self.env.context.get('order_id'))
   return warranty_request.action_add_from_catalog()

This function ensures that when the catalog button is clicked, it properly interacts with the parent model.

Step 4: Add the _get_product_catalog_order_data Function in the Parent Model

The _get_product_catalog_order_data method in the warranty.request model retrieves product details for the catalog, ensuring that only relevant product data is displayed.

def _get_product_catalog_order_data(self, products, **kwargs):
   res = super()._get_product_catalog_order_data(products, **kwargs)
   for product in products:
       res[product.id] |= {
           'price': product.standard_price,
       }
   return res

In this function:

The super() call ensures that the base functionality is preserved.

* The product's standard price is added to the catalog display.

* You can customize this function further to display additional product details as needed.

This implementation enables the catalog to fetch and display relevant product data dynamically.

How to Add the Catalog Feature in Odoo 18-cybrosys

Step 5: Display Already Added Products in the Catalog View

To ensure that users can see the products already added to the One2many field within the catalog view, implement the following methods in the parent and child models.

a) Add _get_product_catalog_record_lines Method in the Parent Model (warranty.request)

This method groups the warranty request lines by product, ensuring that products already added to the warranty.request.line model are reflected in the catalog view.

def _get_product_catalog_record_lines(self, product_ids, child_field=False, **kwargs):
   grouped_lines = defaultdict(lambda: self.env['warranty.request.line'])
   for line in self.warranty_line_ids:
       if line.product_id.id not in product_ids:
           continue
       grouped_lines[line.product_id] |= line
   return grouped_lines
* This function ensures that only products already added to the warranty request are considered in the catalog.
* It returns a dictionary grouping the records by product ID.

b) Add _get_product_catalog_lines_data Method in the Child Model (warranty.request.line)

This method provides details for warranty request lines when interacting with the catalog. If no product exists in the warranty request line, it defaults to quantity = 0. Otherwise, it fetches the quantity and price from the warranty.request.line model.
def _get_product_catalog_lines_data(self):
   catalog_info = {
       'quantity': self.quantity or 0,
       'price': self.product_id.standard_price,
   }
   return catalog_info

* If the product is not added yet, it initializes the quantity as 0.

* If the product is already added, it retrieves the correct quantity and price.

With these methods in place:

* The catalog view will now show products already added to the warranty request line.

* Users can remove or add products dynamically from the catalog view, ensuring better product management.

How to Add the Catalog Feature in Odoo 18-cybrosys

This approach improves the catalog's usability by preventing duplicate product selections and providing clear visibility into the products already included in the warranty request line.

Step 6: Update Newly Added Products from the Catalog View to the Warranty Line

To ensure that products added from the catalog view are properly reflected in the warranty request line, we need to update or create new records accordingly.

Add _update_order_line_info Method in the Parent Model (warranty.request)

This method updates the warranty request line when a user selects a product in the catalog view.

def _update_order_line_info(self, product_id, quantity, **kwargs):
   """
   Update warranty request line information for a given product.
   :param int product_id: The product ID (`product.product`).
   :param float quantity: The quantity to update.
   :return: The updated warranty request line or None if removed.
   """
   self.ensure_one()
   warranty_line = self.warranty_line_ids.filtered(
       lambda line: line.product_id.id == product_id)
   if warranty_line:
       if quantity != 0:
           warranty_line.quantity = quantity
       else:
           warranty_line.unlink()  # Remove line if quantity is set to zero
   elif quantity > 0:
       warranty_line = self.env['warranty.request.line'].create({
           'warranty_id': self.id,
           'product_id': product_id,
           'quantity': quantity,
       })
   return warranty_line

How to Add the Catalog Feature in Odoo 18-cybrosys

* Checks for an existing product in the warranty request line.

* Updates the quantity if the product is already in the list.

* Removes the product if the quantity is set to zero.

* Creates a new entry if the product is not yet added.

How to Add the Catalog Feature in Odoo 18-cybrosys

* When a user adds a new product from the catalog, it appears in the warranty request line.

* If a user updates the quantity, the existing record is modified accordingly.

* If a user sets the quantity to zero, the product is removed from the warranty request line.

This ensures that the catalog and the warranty request line remain in sync

Step 7: Filter Products in the Catalog

To display only specific products in the catalog, we can define the _get_product_catalog_domain method in the parent model. This method filters products based on certain conditions.

def _get_product_catalog_domain(self):
   """Define the product filter for the catalog."""
   return [
       ('company_id', 'in', [self.company_id.id, False]),
       ('detailed_type', '=', 'product')
   ]

How to Add the Catalog Feature in Odoo 18-cybrosys

This ensures that only storable products appear in the catalog, improving product selection accuracy.

Step 8: Add Extra Context to the Catalog View

To enhance the catalog search, we can include additional filtering criteria. The _get_action_add_from_catalog_extra_context method customizes the catalog view by setting a default search filter based on the vendor.

def _get_action_add_from_catalog_extra_context(self):
   """Add extra context to filter catalog search by vendor."""
   return {
       **super()._get_action_add_from_catalog_extra_context(),
       'search_default_seller_ids': self.partner_id.name
   }

By default, the catalog will now filter products based on the selected partner (vendor) in the warranty request form. This improves efficiency by displaying relevant products directly.

How to Add the Catalog Feature in Odoo 18-cybrosys

Adding a catalog feature in Odoo 18 streamlines product selection, making it more intuitive and efficient. By integrating the product.catalog.mixin, adding necessary views and methods, and implementing filtering and contextual search, we can customize the catalog for various business needs.

To read more about how to add the Catalog Feature in Odoo 17, refer to our blog How to Add the Catalog Feature in Odoo 17.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message