Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Odoo 8 Select Product and Variant in seperate fields in Sales order

By
Herman
on 4/8/15, 3:17 PM 1,599 views

Hi there fellow developers

in Odoo 8's product.py I see the name_search method concatenates the product name and variant as the product name. I however have hundreds of attributes to my product.

Height: 500mm, 505mm, ... 3000mm

Width: 500mm, 505mm, ... 4000mm

So it makes it impossible to search the desired variant

So i need first select the product, Then the Height attribute, Then the Width attribute

In Theory I need to override the search method

  • remove the variant in product name

  • create a search method to select first attribute

  • create another search method to select second attribute

  • then it needs to return the product name + att1 + att2 to main search method

Can anyone point me in the right direction?

I really need some serious assistance. Speak to me about cash for code if really neccesary :)


My .py


from openerp import models, fields, api

class product_template(models.Model):

      _inherit = 'product.template'

      palepoint = fields.Char("Pale Point", size=20)

      materialoptions_id = fields.Many2one("product.category","Material Options")

class Sale_order(models.Model):

      _inherit = 'sale.order'

      material_id= fields.Many2one("product.template","Material",domain=[('categ_id.name','=','Raw Material')])

class product_product(models.Model):

      _inherit = 'product.product'

     #This search filter the products by selected product category which works fine

      def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):

          if context is None:

              context = {}

          if context.get('material_id'):

              productobj = self.pool.get('product.product').read(cr, uid, context['material_id'],['materialoptions_id'])

              args = [('categ_id','=',productobj['materialoptions_id'][0])] + args

              return super(product_product, self).search(cr, uid, args, offset, limit, order, context=context, count=count)

My view


<openerp>

      <data>

          <record model="ir.ui.view" id="partner_palepoint_form_view">

              <field name="model">product.template</field>

              <field name="inherit_id" ref="product.product_template_only_form_view"/>

              <field name="arch" type="xml">

              <field name="list_price" position="after">

              <field name="materialoptions_id"/>

              </field>

              <notebook position="inside">

                  <page string="Pale Point">

                      <group>

                          <field name="palepoint"/>

                      </group>

                  </page>

                 </notebook>

              </field>

          </record>

          <record model="ir.ui.view" id="sale_view_form_view">

              <field name="model">sale.order</field>

              <field name="inherit_id" ref="sale_stock.view_order_form_inherit"/>

              <field name="arch" type="xml">

                  <field name="partner_id" position="after">

                      <field name="material_id"/>

              </field>

              <xpath expr="//field[@name='product_id']" position="attributes">

                  <attribute name="context">{'material_id': parent.material_id}</attribute>

              </xpath>

              </field>

          </record>

      </data>

</openerp>



And good old Odoo 8's product.py nam_search method


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

# Only use the product.product heuristics if there is a search term and the domain

# does not specify a match on `product.template` IDs.

if not name or any(term[0] == 'id' for term in (args or [])):

return super(product_template, self).name_search(

cr, user, name=name, args=args, operator=operator, context=context, limit=limit)

product_product = self.pool['product.product']

results = product_product.name_search(

cr, user, name, args, operator=operator, context=context, limit=limit)

product_ids = [p[0] for p in results]

template_ids = [p.product_tmpl_id.id

for p in product_product.browse(

cr, user, product_ids, context=context)]

# re-apply product.template order + name_get

return super(product_template, self).name_search(

cr, user, '', args=[('id', 'in', template_ids)],

operator='ilike', context=context, limit=limit)


1
Herman
On 4/9/15, 5:48 AM

So what I think the best "Work Around" would be is if I create 3 custom fields

  • Product Type (All the product names in selected category)

  • Width (attributes linked to product selected in 'Product Type')

  • Height (attributes linked to product selected in 'Product Type')

above the product field. Then by concatenating those 3 values I can pass the context 'name' to the product search method to match. Therefore only displaying one product matching the criteria above

Hope this helps -me-

Why is this the best way? probably shouldve added this. educational value and what not.

Because, whilst debugging you'll see the product and it's attribute shares an id. That is because odoo treats each product variant as a product. There's no "main" product and then it's child variants. It's one in the same.


LOOK HERE===================================================
{'lang': 'en_US', 'tz': False, 'uid': 1, 'material_id': 19}
LOOK HERE3===================================================
{'lang': 'en_US', 'tz': False, 'uid': 1, 'material_id': 19}
[33, 34, 35, 36]
LOOK HERE2===================================================
{'default_code': False, 'id': 33, 'name': u'Palisade Fence (Domestic 110mm) (1800, 3000mm)'}
LOOK HERE3===================================================
{'lang': 'en_US', 'tz': False, 'uid': 1, 'material_id': 19}
[33, 34, 35, 36]
LOOK HERE2===================================================
{'default_code': False, 'id': 34, 'name': u'Palisade Fence (Domestic 110mm) (1800, 3005mm)'}
LOOK HERE3===================================================
{'lang': 'en_US', 'tz': False, 'uid': 1, 'material_id': 19}
[33, 34, 35, 36]
LOOK HERE2===================================================
{'default_code': False, 'id': 35, 'name': u'Palisade Fence (Domestic 110mm) (1805, 3000mm)'}
LOOK HERE3===================================================
{'lang': 'en_US', 'tz': False, 'uid': 1, 'material_id': 19}
[33, 34, 35, 36]

Which is why each variant has it's own Bill of materials (Which is exactly what i need, Thank you brilliant odoo developers) So unless you want to change the structure of odoo, you first need to pass a context to filter the products, not the other way around

Hope this makes sense everyone. I'm VERY new to programming so please tell me if I'm missing the plot.

odoo is INCREDIBLE thanks guys

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/8/15, 3:17 PM
Seen: 1599 times
Last updated: 4/9/15, 6:58 AM