mirror of
https://github.com/foomo/shop.git
synced 2026-12-04 18:51:53 +00:00
qty threshold on pricerule
This commit is contained in:
+37
-7
@@ -419,6 +419,34 @@ func getOrderTotal(articleCollection *ArticleCollection) float64 {
|
||||
return total
|
||||
}
|
||||
|
||||
// find what is the articleCollection total qty of positions that belong to group
|
||||
// previouslyAppliedDiscounts is for qty 1
|
||||
func getTotalQuantityForPriceRule(priceRule *PriceRule, calculationParameters *CalculationParameters) float64 {
|
||||
var total float64
|
||||
|
||||
for _, article := range calculationParameters.articleCollection.Articles {
|
||||
productGroupIDs := calculationParameters.productGroupIDsPerPosition[article.ID]
|
||||
|
||||
// rule has no customer or product group limitations
|
||||
if len(priceRule.IncludedProductGroupIDS) == 0 &&
|
||||
len(priceRule.ExcludedProductGroupIDS) == 0 &&
|
||||
len(priceRule.IncludedCustomerGroupIDS) == 0 &&
|
||||
len(priceRule.ExcludedCustomerGroupIDS) == 0 {
|
||||
total += article.Quantity
|
||||
} else {
|
||||
//only sum up if limitations are matched
|
||||
if IsOneProductOrCustomerGroupInIncludedGroups(priceRule.IncludedProductGroupIDS, productGroupIDs) &&
|
||||
IsNoProductOrGroupInExcludeGroups(priceRule.ExcludedProductGroupIDS, productGroupIDs) &&
|
||||
IsOneProductOrCustomerGroupInIncludedGroups(priceRule.IncludedCustomerGroupIDS, calculationParameters.groupIDsForCustomer) &&
|
||||
IsNoProductOrGroupInExcludeGroups(priceRule.ExcludedCustomerGroupIDS, calculationParameters.groupIDsForCustomer) {
|
||||
|
||||
total += article.Quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// check if all rule group IDs are included
|
||||
func IsOneProductOrCustomerGroupInIncludedGroups(ruleIncludeGroups []string, productAndCustomerGroups []string) bool {
|
||||
if len(ruleIncludeGroups) == 0 {
|
||||
@@ -523,6 +551,14 @@ func validatePriceRule(priceRule PriceRule, checkedPosition *Article, calculatio
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//check the pricerule qty threshold
|
||||
if priceRule.QtyThreshold > 0 {
|
||||
ruleQtyThreshold := getTotalQuantityForPriceRule(&priceRule, calculationParameters)
|
||||
if ruleQtyThreshold < priceRule.QtyThreshold {
|
||||
return false, ValidationPriceRuleQuantityThresholdNotMet
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
@@ -533,12 +569,7 @@ func validatePriceRule(priceRule PriceRule, checkedPosition *Article, calculatio
|
||||
|
||||
if checkedPosition == nil {
|
||||
for _, article := range calculationParameters.articleCollection.Articles {
|
||||
//the continue here is now obsolete
|
||||
if checkedPosition != nil {
|
||||
if checkedPosition.ID != article.ID {
|
||||
continue
|
||||
}
|
||||
}
|
||||
//at least some must match
|
||||
if IsOneProductOrCustomerGroupInIncludedGroups(priceRule.IncludedProductGroupIDS, calculationParameters.productGroupIDsPerPosition[article.ID]) {
|
||||
productGroupIncludeMatchOK = true
|
||||
}
|
||||
@@ -548,7 +579,6 @@ func validatePriceRule(priceRule PriceRule, checkedPosition *Article, calculatio
|
||||
if IsOneProductOrCustomerGroupInIncludedGroups(priceRule.IncludedCustomerGroupIDS, calculationParameters.groupIDsForCustomer) {
|
||||
customerGroupIncludeMatchOK = true
|
||||
}
|
||||
|
||||
if IsNoProductOrGroupInExcludeGroups(priceRule.ExcludedCustomerGroupIDS, calculationParameters.groupIDsForCustomer) {
|
||||
customerGroupExcludeMatchOK = true
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ const (
|
||||
ValidationPriceRuleOK TypeRuleValidationMsg = "price_rule_ok"
|
||||
|
||||
ValidationPriceRuleCheckoutAttributesMismatch TypeRuleValidationMsg = "pricerule_checkout_attributes_missmatch"
|
||||
ValidationPriceRuleQuantityThresholdNotMet = "qty_below_pricerule_quantity_threshold"
|
||||
)
|
||||
|
||||
//------------------------------------------------------------------
|
||||
|
||||
@@ -87,6 +87,8 @@ type PriceRule struct {
|
||||
|
||||
WhichXYFree XYWhichType
|
||||
|
||||
QtyThreshold float64 // - the total qty in order for the price rule to be applicable. defaults to 0.
|
||||
|
||||
ScaledAmounts []ScaledAmountLevel //defines discount scale 100 -> 2%, 200 -> 3% etc - See ActionScaledPercentage & ActionScaledAbsolute
|
||||
|
||||
ScaledAmountsPerQuantity []ScaledAmountLevel
|
||||
@@ -146,6 +148,7 @@ func NewPriceRule(ID string) *PriceRule {
|
||||
priceRule.Action = ActionItemByPercent
|
||||
priceRule.Amount = 0
|
||||
priceRule.MinOrderAmount = 0
|
||||
priceRule.QtyThreshold = 0
|
||||
priceRule.MaxUses = MaxInt
|
||||
priceRule.MaxUsesPerCustomer = MaxInt
|
||||
priceRule.Exclusive = false
|
||||
|
||||
@@ -123,6 +123,7 @@ func testVoucherRuleWithCheckoutAttributes(t *testing.T) {
|
||||
priceRule.IncludedProductGroupIDS = []string{group.ID}
|
||||
priceRule.IncludedCustomerGroupIDS = []string{}
|
||||
priceRule.CheckoutAttributes = []string{PaymentMethodID1}
|
||||
priceRule.QtyThreshold = 2.0
|
||||
priceRule.Upsert()
|
||||
|
||||
//create pricerule
|
||||
@@ -142,19 +143,19 @@ func testVoucherRuleWithCheckoutAttributes(t *testing.T) {
|
||||
positionVo := &Article{}
|
||||
positionVo.ID = ProductID1SKU1
|
||||
positionVo.Price = 100
|
||||
positionVo.Quantity = 2
|
||||
positionVo.Quantity = 1
|
||||
orderVo.Articles = append(orderVo.Articles, positionVo)
|
||||
|
||||
positionVo = &Article{}
|
||||
positionVo.ID = ProductID1SKU2
|
||||
positionVo.Price = 300
|
||||
positionVo.Quantity = float64(2)
|
||||
positionVo.Quantity = float64(1)
|
||||
orderVo.Articles = append(orderVo.Articles, positionVo)
|
||||
|
||||
positionVo = &Article{}
|
||||
positionVo.ID = ProductID3SKU2
|
||||
positionVo.Price = 500
|
||||
positionVo.Quantity = float64(2)
|
||||
positionVo.Quantity = float64(1)
|
||||
orderVo.Articles = append(orderVo.Articles, positionVo)
|
||||
|
||||
discountsVo, summary, err := ApplyDiscounts(orderVo, nil, []string{VoucherCode1}, []string{PaymentMethodID1}, 0.05, nil)
|
||||
|
||||
Reference in New Issue
Block a user