Odoo e-commerce customization – Odoo offers powerful e-commerce features, but sometimes you need to tailor them to your specific business needs. Today, we’ll explore how to customize product visibility based on customer location and set minimum order quantities for products in your Odoo webshop.
Restricting Product Visibility by Country
Odoo e-commerce customization – First, let’s tackle the challenge of showing specific products only to customers from certain countries. This feature can be useful for region-specific products or compliance with international trade regulations.
Setting Up Country-Specific Products
To begin, we’ll add a new field to our product template:
# In the product.template model
country_ids = fields.Many2many('res.country', string='Allowed Countries')
This code creates a many-to-many relationship between products and countries, allowing you to specify which countries can see each product.
Implementing Visibility Rules
Next, we need to create a record rule to filter products based on the user’s country:
[('country_ids', '=', False), '|', ('country_ids', 'in', user.country_id.id)]
This rule shows products if either no countries are specified (available everywhere) or if the user’s country is in the list of allowed countries.
Handling Public Users
For public (non-logged-in) users, we need an additional step. Odoo can use GeoIP to determine the visitor’s country based on their IP address. You’ll need to implement custom logic to apply this information to the product filter for public users.
Setting Minimum Order Quantities
Now, let’s address how to set and enforce minimum order quantities for products in your webshop.
Adding a Minimum Quantity Field
First, add a new field to your product template:
# In the product.template model
min_quantity = fields.Integer(string='Minimum Order Quantity', default=1)
This allows you to set a minimum order quantity for each product.
Enforcing Minimum Quantities in the Webshop
To enforce this in the webshop, you’ll need to modify the add-to-cart functionality. Here’s a simplified example of how you might do this using JavaScript:
odoo.define('custom_module.product_min_quantity', function (require) {
"use strict";
var publicWidget = require('web.public.widget');
publicWidget.registry.WebsiteSale.include({
_onClickAdd: function (ev) {
var $form = $(ev.currentTarget).closest('form');
var quantity = parseFloat($form.find('input[name="add_qty"]').val() || 1);
var minQuantity = parseFloat($form.find('input[name="min_quantity"]').val() || 1);
if (quantity < minQuantity) {
ev.preventDefault();
this.displayNotification({
title: _t("Minimum Quantity"),
message: _.str.sprintf(
_t("The minimum order quantity for this product is %s."),
minQuantity
),
type: 'warning',
});
return;
}
return this._super.apply(this, arguments);
},
});
});
This code intercepts the add-to-cart action, checks if the quantity meets the minimum, and displays a warning if it doesn’t.
Considerations and Next Steps
While these customizations add powerful features to your Odoo webshop, keep in mind a few important points:
- Country-based restrictions may affect your site’s SEO. Search engines might not index products hidden from certain countries.
- Ensure your minimum quantity restrictions are clearly communicated to customers to avoid confusion.
- Test thoroughly, especially with different user types and locations, to ensure everything works as expected.
By implementing these customizations, you can create a more tailored shopping experience for your customers while maintaining control over your product distribution and order sizes.
For more advanced e-commerce customizations in Odoo, check out the official Odoo e-commerce documentation. Happy coding!
Discover more from teguhteja.id
Subscribe to get the latest posts sent to your email.