Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
1046 Visualizzazioni

Hello every one, I'm trying to filter product selection on the Order Lines based on Customer as I have a Customer field on the Product model. 

When select customer on the Order, then create a new line in Order Lines, the product field will be filtered by Customer 

I have been searching for Context and Domain but I still cant get it work. 

Avatar
Abbandona
Risposta migliore

Hi Phuocnguyen.studio 

To get products based on selected customer in order line first you need to pass the context on tree view of order line at product_id filed like below in XML.

Please find code in comment. 

I hope this will help you.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Abbandona

<xpath expr="//field[@name='product_id']" position="attributes">
<attribute name="context">{
'partner_id': parent.partner_id,
}
</attribute>
</xpath>

Then after doing this add below code inside product.product model.

@api.model
def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None):
product_ids = []
if self._context.get("partner_id") and not self._context.get('product_id'):
partner_id = self.env["res.partner"].browse(self._context["partner_id"])
product_ids = self.with_context(product_id=True).search([('partner_id', '=', partner_id)])
return set(product_ids)
return super()._search(args, offset, limit, order, count, access_rights_uid)