是否可以在Satchmo中根据特定类别或特定于产品配置付款方式?

时间:2011-07-15 09:19:33

标签: django satchmo

我有一个Satchmo供电的商店,需要有一个特殊的商品类别,仅适用于通过货到付款方式付款的用户。

如果没有对结帐流程进行硬编码,我是否可以通过简单的方法将特定产品类别的付款方式限制为仅限现金交付?

2 个答案:

答案 0 :(得分:1)

解决方案是Satchmo几乎为每个动作发出信号,因此在构建支付方法时,您必须听取特定信号,然后重新定义方法 kwarg变量,该变量将传递给侦听器:

from payment.signals import payment_methods_query

def on_payment_methods_query(sender, methods=None, cart=None, order=None, contact=None, **kwargs):
    if not cart.is_empty:
        for item in cart.cartitem_set.all():
            special_products = settings.SPECIAL_PRODUCTS #(1, 15, 25, 75)
            if item.product_id in special_products:
                # methods is a list of (option_value, option_label) tuples
                methods = [m for m in methods if m[0] in ('COD',)]
                return
payment_methods_query.connect(on_payment_methods_query)

答案 1 :(得分:0)

上一个答案中有一个问题(我知道因为我只是尝试过自己),在以下行中:

methods = [m for m in methods if m[0] in ('COD',)] # won't have the desired effect

问题是,在原始方法列表中,创建了一个全新的列表,并存储在相同的变量名称中。这不会影响Satchmo传入的原始列表,因此Satchmo甚至不知道。 您需要做的是实际修改传入的列表对象,使用' methods.remove(...)'等方法。

在特定示例中,它应该是这样的:

disallowed_methods = [m for m in methods if m[0] not in ('COD',)]
for m in disallowed_methods:
    methods.remove(m)

也许我的代码不是最优雅的;也许有人可以改进它,并可能将其与原始答案相结合。