Adding an “Accept Terms” Requirement to Satchmo Checkout

Many stores require that you formally accept their terms and conditions of sale before checking out. How do you pull this off in Satchmo?

It is easy, dead easy, and requires just three lines of code and absolutely no hacking of the Satchmo core. It is all done through the magic of Django Signals.

In my Satchmo Video Presentation, I claimed that signals and templatetags were the key to extending Satchmo without having to build everything into the base store code. This is an example of doing just that.

The Signal

Satchmo sends a “satchmo.payment.signals.payment_form_init” signal when it is initializing payment forms. This gives any interested party the ability to modify the form before it ever gets sent to the template.

This signal can be used for all sorts of interesting purposes. For example, perhaps you want to removed “required” from some of the fields which are required by default. Or perhaps you want to hide certain fields (see payment.listeners.shipping_hide_if_one for an example of this). This signal is the key to doing anything like that.

The Listener

It isn’t a standard Django term, “listener,” but in Satchmo we generally call packages of functions which work with signals “listeners.” It makes sense, there is a signal, and a function who is interested in listening for that signal and acting upon it.

In satchmo.payment.listeners there is a function called “form_terms_listener.” This handy little function will add a new required “terms” field to any form. Exactly what we need!

Take a look at the function. It does require that you have a “shop_terms” named url somewhere in your urlconf. That’s the only prerequisite for its use.

Wiring them up

In my project layout article, I advise that you set up a “site” application for your shop. This is the most convenient place to wire custom signals to listeners. In your storename/site/models.py file, add the following:

from satchmo.payment.forms import SimplePayShipFormfrom satchmo.payment.listeners import form_terms_listenerfrom satchmo.payment.signals import payment_form_init

payment_form_init.connect(form_terms_listener, sender=SimplePayShipForm)

Adding the checkbox to the template

You’ll need to override the page where you are adding the checkbox. Refer to my how to skin a store article if you are unclear how to do this.

In your overridden form template, add the checkbox markup. Something like this:

{{ form.terms }} {{ form.terms.label }}{% if form.terms.error %}<br/>**{{ form.terms.error }}{% endif %}

Done!

Updated 10 July 2010: corrected imports for latest Satchmo

Speak Your Mind

*