【问题标题】:SQL Promotion and Rules tableSQL 提升和规则表
【发布时间】:2018-08-22 17:09:33
【问题描述】:

我正在尝试为电子商务商店设置一些动态促销(在管理区域中管理)。

例如:如果客户在购物车中有棒球帽和棒球面具,并且购物车总和大于 100 美元,则为他提供 20 美元的折扣

我有以下表格结构(可以更改):

PROMOTIONS (id, discount)
PROMOTIONS_RULES (id, promotions_id, variable, operator, value)

我已经用以下虚拟数据填充了表格:

PROMOTIONS
1, 20$

PROMOTIONS_RULES
1, 1, product_id, =, 2
1, 1, product_id, =, 3
1, 1, cart_sum, >, 100

我的问题是:如何有效地为特定的购物车组合寻找合适的促销活动?

我有一系列产品 ID 和购物车总和。而且我需要获得合适的促销折扣。

谢谢大家帮忙!

【问题讨论】:

  • 您能否将您当前的代码提供给我们,以便为您提供更好的帮助?
  • 实际上没有代码。我正在考虑如何在查询中解决这个问题。

标签: php mysql sql


【解决方案1】:

不是真正直接回答您的问题,但我可能会选择捆绑产品本身并在捆绑价格中打折它们的总价值。

编辑:

画了一个小草图,可能会帮助或引导你走向某个方向。

//table structure something like this
discounts
id_discount, amount, name

discount_rules 
id_rule, id_discount, combination (bool), min_cart_total_value, active (bool)

discount_combination
id_combination, id_rule

discount_combination_products
id_combination, id_product

// query for combination discounts when in shopping cart
Select d.id_discount
From discounts d
Left Join discount_rules dr on d.id_discount = dr.id_discount
Where dr.active = 1, dr.combination = 1

//sort the cart products
sort($cartproducts);

foreach($queried_ids as $discount_id){
//query for products in those combination discounts
Select dcp.id_product
From discount_rules dr
Left Join discount_combination dc on dr.id_rule = dc.id_rule
Left Join discount_combination_products dcp on dc.id_combination = dcp.id_combination
Where dr.id_discount = $discount_id

//match the cart with possible discounts
sort($result_array);
if($cartproducts == $result_array){
//match apply this discount to the cart and so on.
}
}

还建议你检查一下现有的电子商务平台,看看他们是如何做到的。

【讨论】:

  • 好吧,如果有人订购了 2 顶棒球帽(因此金额将超过特定值),我们可能想给他 25 美元的折扣(仅作为示例),捆绑的想法是第一个一。但我们希望有一个工具来管理更多规则的折扣。那是因为我们不想使用静态包。
  • 编辑了我的答案,希望草图对您的思考过程有所帮助。
  • 非常感谢,它对我帮助很大。我已经通过 php 解决了这个问题。好像不能用纯mysql查询促销。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多