Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
feat(kit-subtotal-discount): handle 'discount_kit_subtotal' option
Browse files Browse the repository at this point in the history
as discount by subtotal, but considering only kit items
  • Loading branch information
leomp12 committed Aug 19, 2020
1 parent c356566 commit 14382ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
5 changes: 5 additions & 0 deletions assets/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@
"title": "Descontar menor preço",
"description": "Aplicar desconto com valor igual ao menor preço entre os produtos selecionados"
},
"discount_kit_subtotal": {
"type": "boolean",
"title": "Descontar subtotal do kit",
"description": "Aplicar desconto em função do subtotal somando apenas os produtos do kit"
},
"discount": {
"title": "Desconto predefinido",
"type": "object",
Expand Down
32 changes: 21 additions & 11 deletions routes/ecom/modules/apply-discount.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,29 @@ const getValidDiscountRules = (discountRules, items) => {
return false
}

if (Array.isArray(rule.product_ids) && rule.discount_lowest_price && Array.isArray(items)) {
if (Array.isArray(rule.product_ids) && Array.isArray(items)) {
// set/add discount value from lowest item price
let value
items.forEach(item => {
const price = ecomUtils.price(item)
if (
price > 0 &&
rule.product_ids.indexOf(item.product_id) > -1 &&
(!value || value > price)
) {
value = price
}
})
if (rule.discount_lowest_price) {
items.forEach(item => {
const price = ecomUtils.price(item)
if (
price > 0 &&
rule.product_ids.indexOf(item.product_id) > -1 &&
(!value || value > price)
) {
value = price
}
})
} else if (rule.discount_kit_subtotal) {
value = 0
items.forEach(item => {
const price = ecomUtils.price(item)
if (price > 0 && rule.product_ids.indexOf(item.product_id) > -1) {
value += price * item.quantity
}
})
}
if (value) {
if (rule.discount && rule.discount.value) {
if (rule.discount.type === 'percentage') {
Expand Down

0 comments on commit 14382ff

Please sign in to comment.