feat: Slashed Prices and Discount display

- Registered mrp and price after discounts
- slashed price with discount in listing, item page and wishlist
- removed redundant imports
- renamed method to `get_web_item_qty_in_stock` to get Website Item stock
- adjusted styles for resizing
- made add to cart button full width on cards
This commit is contained in:
marination 2021-04-13 00:39:26 +05:30
parent 3d5f029e51
commit 60261852b2
11 changed files with 138 additions and 73 deletions

View File

@ -3,7 +3,7 @@
# For license information, please see license.txt
import frappe
from frappe.utils import cint
from frappe.utils import cint, comma_and
from frappe import _, msgprint
from frappe.model.document import Document
from frappe.utils import cint

View File

@ -67,9 +67,13 @@ class ProductQuery:
# add price and availability info in results
for item in result:
product_info = get_product_info_for_website(item.item_code, skip_quotation_creation=True).get('product_info')
if product_info:
item.formatted_price = (product_info.get('price') or {}).get('formatted_price')
item.price = (product_info.get('price') or {}).get('price_list_rate')
if product_info and product_info['price']:
item.formatted_mrp = product_info['price'].get('formatted_mrp')
item.formatted_price = product_info['price'].get('formatted_price')
if item.formatted_mrp:
item.discount = product_info['price'].get('formatted_discount_percent') or \
product_info['price'].get('formatted_discount_rate')
item.price = product_info['price'].get('price_list_rate')
if self.settings.show_stock_availability:
if item.get("website_warehouse"):

View File

@ -11,7 +11,7 @@ from frappe.utils.nestedset import get_root_of
from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import get_shopping_cart_settings
from erpnext.accounts.utils import get_account_name
from erpnext.utilities.product import get_qty_in_stock
from erpnext.utilities.product import get_web_item_qty_in_stock
class WebsitePriceListMissingError(frappe.ValidationError):
@ -93,7 +93,7 @@ def place_order():
item.item_code, ["website_warehouse", "is_stock_item"])
if is_stock_item:
item_stock = get_qty_in_stock(item.item_code, "website_warehouse")
item_stock = get_web_item_qty_in_stock(item.item_code, "website_warehouse")
if not cint(item_stock.in_stock):
throw(_("{1} Not in Stock").format(item.item_code))
if item.qty > item_stock.stock_qty[0][0]:

View File

@ -8,7 +8,7 @@ from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import (
get_shopping_cart_settings,
show_quantity_in_website
)
from erpnext.utilities.product import get_price, get_qty_in_stock, get_non_stock_item_status
from erpnext.utilities.product import get_price, get_web_item_qty_in_stock, get_non_stock_item_status
@frappe.whitelist(allow_guest=True)
def get_product_info_for_website(item_code, skip_quotation_creation=False):
@ -30,8 +30,7 @@ def get_product_info_for_website(item_code, skip_quotation_creation=False):
cart_settings.default_customer_group,
cart_settings.company
)
stock_status = get_qty_in_stock(item_code, "website_warehouse")
stock_status = get_web_item_qty_in_stock(item_code, "website_warehouse")
product_info = {
"price": price,

View File

@ -68,7 +68,7 @@ body.product-page {
.item-card-group-section {
.card {
height: 360px;
height: 400px;
align-items: center;
justify-content: center;
@ -134,6 +134,11 @@ body.product-page {
}
.item-card {
padding: var(--padding-sm);
min-width: 300px;
}
.wishlist-card {
padding: var(--padding-sm);
min-width: 260px;
}
@ -626,8 +631,7 @@ body.product-page {
.btn-explore-variants {
box-shadow: none;
margin: var(--margin-sm) 0;
margin-left: 18px;
max-height: 30px; // to avoid resizing on window resize
max-height: 50px; // to avoid resizing on window resize
flex: none;
transition: 0.3s ease;
color: var(--orange-500);
@ -643,13 +647,12 @@ body.product-page {
.btn-add-to-cart-list{
box-shadow: none;
margin: var(--margin-sm) 0;
max-height: 30px; // to avoid resizing on window resize
max-height: 50px; // to avoid resizing on window resize
flex: none;
transition: 0.3s ease;
}
.not-added {
margin-left: 18px;
color: var(--blue-500);
background-color: white;
border: 1px solid var(--blue-500);
@ -661,7 +664,6 @@ body.product-page {
}
.added-to-cart {
margin-left: 18px;
background-color: var(--dark-green-400);
color: white;
border: 2px solid var(--green-300);

View File

@ -39,7 +39,7 @@
{{ doc.website_content or '' }}
<!-- Reviews and Comments -->
{% if shopping_cart.cart_settings.enable_reviews %}
{% if shopping_cart.cart_settings.enable_reviews and not doc.has_variants %}
{% include "templates/generators/item/item_reviews.html"%}
{% endif %}
</div>

View File

@ -7,9 +7,21 @@
<div class="col-md-12">
<!-- Price and Availability -->
{% if cart_settings.show_price and product_info.price %}
{% set price_info = product_info.price %}
{% if price_info.formatted_mrp %}
<small class="formatted-price">
M.R.P.:
<s>{{ price_info.formatted_mrp }}</s>
</small>
<small class="ml-2 formatted-price" style="color: #F47A7A; font-weight: 500;">
{{ price_info.get("formatted_discount_percent") or price_info.get("formatted_discount_rate")}} OFF
</small>
{% endif %}
<div class="product-price">
{{ product_info.price.formatted_price_sales_uom }}
<small class="formatted-price">({{ product_info.price.formatted_price }} / {{ product_info.uom }})</small>
{{ price_info.formatted_price_sales_uom }}
<small class="formatted-price">({{ price_info.formatted_price }} / {{ product_info.uom }})</small>
</div>
{% else %}
{{ _("UOM") }} : {{ product_info.uom }}

View File

@ -142,31 +142,43 @@
<div class="product-description ellipsis">{{ description or '' }}</div>
{% else %}
<div class="product-category">{{ item.item_group or '' }}</div>
<div style="display: flex;">
{% if item.formatted_price %}
<div class="product-price">{{ item.formatted_price or '' }}</div>
<div class="product-price">
{{ item.formatted_price or '' }}
{% if item.get("formatted_mrp") %}
<small class="ml-1 text-muted">
<s>{{ item.formatted_mrp }}</s>
</small>
<small class="ml-1" style="color: #F47A7A; font-weight: 500;">
{{ item.discount }} OFF
</small>
{% endif %}
</div>
{% endif %}
{% if item.has_variants %}
<a href="/{{ item.route or '#' }}">
<div class="btn btn-sm btn-explore-variants">
<div class="btn btn-sm btn-explore-variants w-100 mt-4">
{{ _('Explore') }}
</div>
</a>
{% elif settings.enabled %}
<div id="{{ item.name }}" class="btn btn-sm btn-add-to-cart-list not-added"
{% elif settings.enabled and (settings.allow_items_not_in_stock or item.in_stock != "red")%}
<div id="{{ item.name }}" class="btn btn-sm btn-add-to-cart-list not-added w-100 mt-4"
data-item-code="{{ item.item_code }}">
{{ _('Add to Cart') }}
</div>
{% endif %}
</div>
{% endif %}
</div>
{%- endmacro -%}
{%- macro wishlist_card(item, settings) %}
<div class="col-sm-3 item-card">
<div class="card text-center">
<div class="col-sm-3 wishlist-card">
<div class="card text-center" style="max-height: 390px;">
{% if item.image %}
<div class="card-img-container">
<a href="/{{ item.route or '#' }}" style="text-decoration: none;">
@ -201,11 +213,22 @@
<div style="margin-top: 16px;">
<div class="product-title">{{ item.item_name or item.item_code or ''}}</div>
</div>
<div class="product-price">{{ item.formatted_price or '' }}</div>
<div class="product-price">
{{ item.formatted_price or '' }}
{% if item.get("formatted_mrp") %}
<small class="ml-1 text-muted">
<s>{{ item.formatted_mrp }}</s>
</small>
<small class="ml-1" style="color: #F47A7A; font-weight: 500;">
{{ item.discount }} OFF
</small>
{% endif %}
</div>
{% if (item.available and settings.show_stock_availability) or (not settings.show_stock_availability) %}
<!-- Show move to cart button if in stock or if showing stock availability is disabled -->
<button data-item-code="{{ item.item_code}}" class="btn btn-add-to-cart w-100 wishlist-cart-not-added">
<button data-item-code="{{ item.item_code}}" class="btn btn-add-to-cart w-100 wishlist-cart-not-added mt-2">
<span class="mr-2">
<svg class="icon icon-md">
<use href="#icon-assets"></use>
@ -214,7 +237,7 @@
{{ _("Move to Cart") }}
</button>
{% else %}
<div style="color: #F47A7A; width: 100%;">
<div class="mt-4" style="color: #F47A7A; width: 100%;">
{{ _("Not in Stock") }}
</div>
{% endif %}

View File

@ -7,7 +7,7 @@
{% block page_content %}
{% if items %}
<div class="row">
<div class="col-12 col-md-11 item-card-group-section">
<div class="col-md-12 item-card-group-section">
<div class="row products-list">
{% from "erpnext/templates/includes/macros.html" import wishlist_card %}
{% for item in items %}

View File

@ -5,26 +5,45 @@ from __future__ import unicode_literals
no_cache = 1
import frappe
from erpnext.utilities.product import get_price
from erpnext.e_commerce.shopping_cart.cart import _set_price_list
def get_context(context):
settings = frappe.get_doc("E Commerce Settings")
items = get_wishlist_items()
selling_price_list = _set_price_list(settings)
if settings.show_stock_availability:
for item in items:
stock_qty = frappe.utils.flt(
frappe.db.get_value("Bin",
{
"item_code": item.item_code,
"warehouse": item.get("warehouse")
},
"actual_qty")
if settings.show_stock_availability:
item.available = get_stock_availability(item.item_code, item.get("warehouse"))
price_details = get_price(
item.item_code,
selling_price_list,
settings.default_customer_group,
settings.company
)
item.available = True if stock_qty else False
if price_details:
item.formatted_mrp = price_details.get('formatted_mrp')
if item.formatted_mrp:
item.discount = price_details.get('formatted_discount_percent') or \
price_details.get('formatted_discount_rate')
context.items = items
context.settings = settings
def get_stock_availability(item_code, warehouse):
stock_qty = frappe.utils.flt(
frappe.db.get_value("Bin",
{
"item_code": item_code,
"warehouse": warehouse
},
"actual_qty")
)
return True if stock_qty else False
def get_wishlist_items():
if frappe.db.exists("Wishlist", frappe.session.user):
return frappe.db.sql("""

View File

@ -1,24 +1,21 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe
from frappe.utils import cint, flt, fmt_money, getdate, nowdate
from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
from erpnext.stock.doctype.batch.batch import get_batch_qty
def get_qty_in_stock(item_code, item_warehouse_field, warehouse=None):
def get_web_item_qty_in_stock(item_code, item_warehouse_field, warehouse=None):
in_stock, stock_qty = 0, ''
template_item_code, is_stock_item = frappe.db.get_value("Item", item_code, ["variant_of", "is_stock_item"])
if not warehouse:
warehouse = frappe.db.get_value("Item", item_code, item_warehouse_field)
warehouse = frappe.db.get_value("Website Item", {"item_code": item_code}, item_warehouse_field)
if not warehouse and template_item_code and template_item_code != item_code:
warehouse = frappe.db.get_value("Item", template_item_code, item_warehouse_field)
warehouse = frappe.db.get_value("Website Item", {"item_code": template_item_code }, item_warehouse_field)
if warehouse:
stock_qty = frappe.db.sql("""
@ -93,17 +90,26 @@ def get_price(item_code, price_list, customer_group, company, qty=1):
"for_shopping_cart": True,
"currency": frappe.db.get_value("Price List", price_list, "currency")
}))
price_obj = price[0]
if pricing_rule:
# price without any rules applied
mrp = price_obj.price_list_rate or 0
if pricing_rule.pricing_rule_for == "Discount Percentage":
price[0].price_list_rate = flt(price[0].price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0)))
price_obj.formatted_discount_percent = str(flt(pricing_rule.discount_percentage, 0)) + "%"
price_obj.price_list_rate = flt(price_obj.price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0)))
if pricing_rule.pricing_rule_for == "Rate":
price[0].price_list_rate = pricing_rule.price_list_rate
rate_discount = flt(mrp) - flt(pricing_rule.price_list_rate)
if rate_discount > 0:
price_obj.formatted_discount_rate = fmt_money(rate_discount, currency=price_obj["currency"])
price_obj.price_list_rate = pricing_rule.price_list_rate or 0
price_obj = price[0]
if price_obj:
price_obj["formatted_price"] = fmt_money(price_obj["price_list_rate"], currency=price_obj["currency"])
if mrp != price_obj["price_list_rate"]:
price_obj["formatted_mrp"] = fmt_money(mrp, currency=price_obj["currency"])
price_obj["currency_symbol"] = not cint(frappe.db.get_default("hide_currency_symbol")) \
and (frappe.db.get_value("Currency", price_obj.currency, "symbol", cache=True) or price_obj.currency) \
@ -124,15 +130,15 @@ def get_price(item_code, price_list, customer_group, company, qty=1):
price_obj["currency"] = ""
if not price_obj["formatted_price"]:
price_obj["formatted_price"] = ""
price_obj["formatted_price"], price_obj["formatted_mrp"] = "", ""
return price_obj
def get_non_stock_item_status(item_code, item_warehouse_field):
#if item belongs to product bundle, check if bundle items are in stock
# if item is a product bundle, check if its bundle items are in stock
if frappe.db.exists("Product Bundle", item_code):
items = frappe.get_doc("Product Bundle", item_code).get_all_children()
bundle_warehouse = frappe.db.get_value('Item', item_code, item_warehouse_field)
return all(get_qty_in_stock(d.item_code, item_warehouse_field, bundle_warehouse).in_stock for d in items)
return all([ get_web_item_qty_in_stock(d.item_code, item_warehouse_field, bundle_warehouse).in_stock for d in items ])
else:
return 1