How i can show many2one field all records on a form view instead drop down list in odoo 10? - odoo-10

How I can show many2one field all records on a form view instead drop down list in odoo 10.
For example, I have a Product category and Products.
When I select Product category from drop down list then all products belongs to that category shows on form view instead of a drop down list.

Here I assume you have category id like,
category_id = fields.Many2one('product.category', "Product Category")
Add Many2many of product.product field to your model for product records.
product_ids = fields.Many2many('product.product', "Products")
Now you need to define onchange on category_id field like.
#api.onchange('category_id')
def onchange_product_category(self):
if self.category_id:
self.product_ids = [(5,)] # unlink all existing records of product
product_recs = self.env['product.product'].search([('categ_id', '=', self.category_id.id)])
self.product_ids = [(4, x) for x in product_recs] # link the product records of selected category
If you do not want to display your product records in list view you can use widget many2many_tags in the form view like,
<field name="product_ids" widget="many2many_tags"/>
It will work for you.

Related

Server Action: Loop through each record of model

I have a server action to update product costs when selected in tree view:
bom_obj = env["mrp.bom"]
for product in object.browse(context.get('active_ids')):
price = 0
bom = bom_obj._bom_find(product=product)
if bom:
price = product._calc_price(bom)
product.write({'standard_price':price})
But This unfortunately only selects records visible in tree view, not ALL the records in product.product
I tried:
bom_obj = env["mrp.bom"]
product_obj = env["product.product"]
product_ids = product_obj.search(cr, uid, [])
for product in product_ids:
price = 0
bom = bom_obj._bom_find(product=product)
if bom:
price = product._calc_price(bom)
product.write({'standard_price':price})
Could you please tell me how to loop through each record of product.product.
OR
instead, tell me how I would go about update price of record selected in m2o field.
I know how to trigger the code in server action, I just need to know how to get record from m2o field. I will use this when product_id is changed in Sale Order Line, to update the price as it is selected.
Thanks

how to display data in custom table in a form view (Odoo)

I want to make some calculations with product data and display them in a custom table.
Specifically what I want to do is:
1. take a list of products of a particular invoice
2. calculate the percentage of the product price to the total value of the invoice
3. display the custom table with the name of the product and the calculated value
for example:
I have a list of the kind:
[{'name': u'Ipad', 'percentage': 45},
{'name': u'Imac', 'percentage': 20}]
How I can display the result in a notebook page of form view? Maybe I can do this with Qweb templates?, any example?, thanks.

How to show region specific products magento?

how to show products in Magento based on selected state and city? is
there any extension available for this feature? or how can i achieve
this?
You can add one (city) or two (state and city) attributes in product model (by back-office panel).
Your news attributes can be contain the list (in example string with delimiter commas) with the list of state/city where the product is allowed to be sell.
For retrieve your state/city allowed list you can :
<?php
// id product is 12345
// the city allowed list attribute code is 'city_list'
$id = 12345;
$product = Mage::getModel('catalog/product')->load($id);
$cityList = $product->getAttributeText('city_list');
var_dump($cityList);
Output something like this :
string(38) "Los Angeles, New Delhi, Caen, etc, etc"
It's possible that you may be add ->getAttributeToSelect('city_list') after load($id) if the attribute isn't retrieve by catalog/product model.

How to show sort by most popular(most sold) products on product listing page in Magento?

I am using following tutorial to display the most sold product sort by option(Filtering) to display on product listing page in magento?
Tutorial
/app/code/local/Mage/Catalog/Model/Resource/Product/collection.php
<?php
public function sortByReview($dir){
$table = $this->getTable('review/review');
$entity_code_id = Mage::getModel('review/review')->getEntityIdByCode(Mage_Rating_Model_Rating::ENTITY_PRODUCT_CODE);
$cond = $this->getConnection()->quoteInto('t2.entity_pk_value = e.entity_id and ','').$this->getConnection()->quoteInto('t2.entity_id = ? ',$entity_code_id);
$this->getSelect()->joinLeft(array('t2'=>$table), $cond,array('review' => new Zend_Db_Expr('count(review_id)')))
->group('e.entity_id')->order("review $dir");
}
?>
But I want to sort products which are most sold from every category.
How can I do this?
Is there any free extension available for this?
I did the following thing as the client did not want automatically filter most sold product.
I created an attribute "popular" as drop down and give values from 1 to 5.Then marked "Used for Sorting in Product Listing" to Yes.
After this the attribute was visible under sorting options.

How to get a list of categories by product number?

I need to sort categoies by product number that means which category has large number of products is shown at the top of category list. So catgories list should be descending order by product number.
any help would be appreciated!
I have categories and its product as following as:
category no of products
cell phones 5
cameras 8
computers 3
I would like to sort categories cameras,cell phones,computers by product number .For this sorting result i would like to use join in collection class of category.
The following code will fetch the product per category:-
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category);
$productCount = $productCollection->count();
You can do in this way:-
Get an array of all categories
Loop through them
Fetch product collection with product count for each category (from the above code).
Sort categories with the product count.
Hope this helps. Thanks.
If you have a collection of categories called $collection then you can do this:
$collection->setOrder('children_count', 'desc');
Edit:
To make it the default in an overridden collection use this method:
protected function _initSelect()
{
parent::_initSelect();
$this->setOrder('children_count', 'desc');
return $this;
}

Resources