Skip to Content
Menu
This question has been flagged
8 Replies
5394 Views

When user types in product field under quotation line item it searches based on product name. How to include Product category in this search? So if the characters entered match with product category only those products under that category should be shown.    

Avatar
Discard
Best Answer

Inherit a product.product and inside write this code.

class product_product(osv.osv):

    _inherit = "product.product"

    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):

        res = super(product_product, self).name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100)

        if operator in ('ilike', 'like', '=', '=like', '=ilike'):

            domain = [('categ_id', operator, name)]

            ids = self.search(cr, user, domain, limit=limit, context=context)

            res += self.name_get(cr, user, ids, context=context)

        return res

Avatar
Discard
Author

Thank you for the quick response. I tried this but did not work. It's still not searching based on product category

sorry here my mistake

domain = [('categ_id', operator, name)]

replace name with categ_id

Author

This is what I did but didn't work. I am using OpenERP 7.

class sale_order_product_search(osv.osv):

_name = "sale.order.product.search"

_inherit = "product.product"

def name_search(self, name, args=None, operator='ilike', limit=100):

res = super(product_product, self).name_search(name, args, operator=operator, limit=limit)

if operator in ('ilike', 'like', '=', '=like', '=ilike'):

domain = [('categ_id', operator, name)]

res += self.search(domain, limit=limit).name_get()

return res

sale_order_product_search()

first don't give class new name and _name only _inherit then it's work

do like this

class product_product(osv.osv):

_inherit = "product.product"

def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):

res = super(product_product, self).name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100)

if operator in ('ilike', 'like', '=', '=like', '=ilike'):

domain = [('categ_id', operator, name)]

ids = self.search(cr, user, domain, limit=limit, context=context)

res += self.name_get(cr, user, ids, context=context)

return res

Author Best Answer

https://pastebin.com/Nj8isCij

This is how I added product category in the quotation line item product search.

Avatar
Discard
Best Answer

You will need to modify the _name_search function of the product.product model. Something similar to this should work:

@api.model
def _name_search(self, name='', args=None, operator='ilike', limit=100):
    if args is None:
        args = []
    domain = args + ['|', ('categ_id', operator, name), ('name', operator, name)]
    return super(ProductProduct, self).search(domain, limit=limit).name_get()

If you only want this change to apply in the quotation page, you will need to use the context:

@api.model
def _name_search(self, name='', args=None, operator='ilike', limit=100):
if self.env.context.get('allow_product_search_by_category'):
    if args is None:
        args = []
    domain = args + ['|', ('categ_id', operator, name), ('name', operator, name)]
    return super(ProductProduct, self).search(domain, limit=limit).name_get()
else:
return super(ProductProduct, self)._name_search(name, args, operator, limit)

And then on the  view definition you'll need to add the context:

<field name="product_id" context="{'allow_product_search_by_category': True}"/>


Avatar
Discard
Author

Thank you for the prompt reply. I tried this but it did not work. There is no field named 'category_id' in the product_product. It has product_templ_id (product_template) which has categ_id (product_category).

I also noticed that product_product::name_search adds any externally passed arguments to the search. So instead of modifying the code can the args be passed from view?

Sorry, their naming conventions always get me. The field is categ_id.

As the product.product inherits the product.template, any field defined on the product.template will exist on the product.product.