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

发布于 2024-11-24 16:50:58 字数 116 浏览 4 评论 0原文

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

除了对结帐流程进行硬编码之外,是否有任何简单的方法可以将特定产品类别的付款选项限制为仅限货到付款?

I have a Satchmo-powered shop that needs to have a special item category available only for users that pay by cash-on-delivery method.

Short of hard-coding the checkout process, is there any easy way I can use to restrict payment options for particular product category to cash-on-delivery only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独岁月 2024-12-01 16:50:58

解决方案是 Satchmo 几乎为每个操作发出信号,因此在构建支付方式表单时,您必须侦听特定信号,然后重新定义传递给侦听器的 methods 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)

Solution is that Satchmo emits signals almost for every action, so on construction of payment methods form you have to listen to a specific signal and then redefine methods kwarg variable which gets passed to listener:

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)
苦妄 2024-12-01 16:50:58

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

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)

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

There is one problem in the previous answer (I know because I just tried myself), in the following line:

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

Problem is, out of the original methods list, a completely new list is created, and stored in the same variable name. This will not affect the original list that Satchmo passed in, so Satchmo won't even know.
What you need to do is actually modify the list object passed in, using methods like 'methods.remove(...)'.

In the particular example, it should be something like this:

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

Maybe my code is not the most elegant; maybe someone can improve it, and possibly integrate it with the original answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文