Search code examples
djangosatchmo

Is it possible to configure payment methods on category-specific or product-specific basis in 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?


Solution

  • 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)