Skip to content

Commit

Permalink
[11.0][IMP] website_sale_product_minimal_price: Order by minimal vari…
Browse files Browse the repository at this point in the history
…ant price (from pricelists or extra variant price)
  • Loading branch information
sergio-teruel committed May 17, 2019
1 parent ec6b072 commit 4735d41
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 33 deletions.
8 changes: 4 additions & 4 deletions website_sale_product_minimal_price/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Website Sale Product Minimal Price
|badge1| |badge2| |badge3| |badge4| |badge5|

This module extends the functionality of website sale module to allow to
display the minimal price in '/shop' view when product has extra variants
price and set order by extra price in product's view.
display the minimal price in '/shop' view when product has distinct variants
price and set order by minimal price in product's view.

**Table of contents**

Expand All @@ -38,10 +38,10 @@ Usage
=====

#. Go to backend and set a product with variants and extra price by attribute
value.
value or define a distinct prices in public price list for this variant.
#. Go to Website Shop.
#. You will see that in main products view appears the text "From " with
minimal price if the product has a diferent extra prices by attribute.
minimal price if the product has a distinct prices by attribute.
#. Click on product, the price displayed is the minimal variant price.

Bug Tracker
Expand Down
28 changes: 11 additions & 17 deletions website_sale_product_minimal_price/models/product_template.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
# Copyright 2019 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
from odoo import fields, models


class ProductTemplate(models.Model):
_inherit = 'product.template'

has_variant_price_extra = fields.Boolean(
compute='_compute_has_variant_price_extra',
store=True,
string='Has variants with price extra',
has_distinct_variant_price = fields.Boolean(
compute='_compute_has_distinct_variant_price',
string='Has variants with distinct extra',
)

@api.depends(
'product_variant_ids.attribute_value_ids.price_ids.price_extra')
def _compute_has_variant_price_extra(self):
def _compute_has_distinct_variant_price(self):
for template in self:
if (template.product_variant_count and len(set(
template.product_variant_ids.mapped('price_extra'))) != 1):
template.has_variant_price_extra = True
if template.product_variant_count > 1:
prices = template.product_variant_ids.mapped('website_price')
if len(prices) > 1:
template.has_distinct_variant_price = True

def _website_price(self):
templates = self.filtered(
lambda x: (
x.product_variant_count > 1 and any(
[v.price_extra != 0.0 for v in x.product_variant_ids]))
)
templates = self.filtered(lambda x: x.product_variant_count > 1)
super(ProductTemplate, self - templates)._website_price()
for product in templates:
variant = product.product_variant_ids.sorted(
key=lambda v: v.price_extra)[:1]
key=lambda p: p.website_price)[:1]
product.website_price = variant.website_price
product.website_public_price = variant.website_public_price
product.website_price_difference = variant.website_price_difference
4 changes: 2 additions & 2 deletions website_sale_product_minimal_price/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
This module extends the functionality of website sale module to allow to
display the minimal price in '/shop' view when product has extra variants
price and set order by extra price in product's view.
display the minimal price in '/shop' view when product has distinct variants
price and set order by minimal price in product's view.
4 changes: 2 additions & 2 deletions website_sale_product_minimal_price/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#. Go to backend and set a product with variants and extra price by attribute
value.
value or define a distinct prices in public price list for this variant.
#. Go to Website Shop.
#. You will see that in main products view appears the text "From " with
minimal price if the product has a diferent extra prices by attribute.
minimal price if the product has a distinct prices by attribute.
#. Click on product, the price displayed is the minimal variant price.
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ <h1 class="title">Website Sale Product Minimal Price</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/e-commerce/tree/11.0/website_sale_product_minimal_price"><img alt="OCA/e-commerce" src="https://img.shields.io/badge/github-OCA%2Fe--commerce-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/e-commerce-11-0/e-commerce-11-0-website_sale_product_minimal_price"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/113/11.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p>This module extends the functionality of website sale module to allow to
display the minimal price in ‘/shop’ view when product has extra variants
price and set order by extra price in product’s view.</p>
display the minimal price in ‘/shop’ view when product has distinct variants
price and set order by minimal price in product’s view.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -388,10 +388,10 @@ <h1 class="title">Website Sale Product Minimal Price</h1>
<h1><a class="toc-backref" href="#id1">Usage</a></h1>
<ol class="arabic simple">
<li>Go to backend and set a product with variants and extra price by attribute
value.</li>
value or define a distinct prices in public price list for this variant.</li>
<li>Go to Website Shop.</li>
<li>You will see that in main products view appears the text “From ” with
minimal price if the product has a diferent extra prices by attribute.</li>
minimal price if the product has a distinct prices by attribute.</li>
<li>Click on product, the price displayed is the minimal variant price.</li>
</ol>
</div>
Expand Down
8 changes: 4 additions & 4 deletions website_sale_product_minimal_price/views/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
<template id="variants" inherit_id="website_sale.variants">
<!-- Sort product attribute values by minimal price extra -->
<xpath expr="//t[@t-if=&quot;variant_id.attribute_id.type == &apos;select&apos;&quot;]//t[@t-foreach='variant_id.value_ids']" position="attributes">
<attribute name="t-foreach">variant_id.value_ids.sorted(key=lambda x: x.price_extra)</attribute>
<attribute name="t-foreach">variant_id.value_ids.sorted(key=lambda x: min(x.product_ids.mapped('website_price')))</attribute>
</xpath>
<xpath expr="//t[@t-if=&quot;variant_id.attribute_id.type == &apos;radio&apos;&quot;]//t[@t-foreach='variant_id.value_ids']" position="attributes">
<attribute name="t-foreach">variant_id.value_ids.sorted(key=lambda x: x.price_extra)</attribute>
<attribute name="t-foreach">variant_id.value_ids.sorted(key=lambda x: min(x.product_ids.mapped('website_price')))</attribute>
</xpath>
<xpath expr="//li[@t-foreach='variant_id.value_ids']" position="attributes">
<attribute name="t-foreach">variant_id.value_ids.sorted(key=lambda x: x.price_extra)</attribute>
<attribute name="t-foreach">variant_id.value_ids.sorted(key=lambda x: min(x.product_ids.mapped('website_price')))</attribute>
</xpath>
</template>

<template id="products_item" inherit_id="website_sale.products_item">
<!-- Display "From" if variants have distinct extra prices -->
<xpath expr="//span[@t-esc='product.website_price']" position="before">
<t t-if="product.has_variant_price_extra">
<t t-if="product.has_distinct_variant_price">
<span>From </span>
</t>
</xpath>
Expand Down

0 comments on commit 4735d41

Please sign in to comment.