Accessing the user's address for a cart modifier

84 views
Skip to first unread message

__underscore

unread,
Dec 19, 2011, 8:54:30 PM12/19/11
to django-shop
Hi,
I'm new to django-shop, and for the most part I'm enjoying it as a
simple ecommerce solution for Django. One thing is currently stumping
me however, so I was wondering if anyone had any tips.

I want to:
1) Allow non-logged-in users to use the shop (so cart.user will be
None in most cases).
2) Only charge sales tax if a buyer lives in a certain state.

I have written a cart modifier with all the logic that I need, except
that for anonymous users I can't get access to their shipping address.
As a result I can't conditionally apply the cart modifier.

Any suggestions? Thanks!

__underscore

unread,
Dec 20, 2011, 6:59:11 PM12/20/11
to django-shop
Alright, I found a somewhat-workable solution, and I'll post the gist
below in case somebody else has the same problem:

1) Override the checkout view so that the request object is passed in
to your cart. I subclassed CheckoutSelectionView to do this as
follows:

class OverriddenCheckoutView(CheckoutSelectionView):
"""
Override django-shop's default checkout view so we can pass
request info to the cart.

For this to be used, you'll need to "shadow" django_shop's default
checkout view in urls.py.
For example::

(r'^store/checkout/$', OverriddenCheckoutView.as_view()),
(r'^store/', include(shop_urls)),
"""
def create_order_object_from_cart(self):
"""
Override the default django_shop implementation, by passing
self.request to the cart.
"""
request = self.request
cart = get_or_create_cart(request)
cart.update(state={'request': request})
order = Order.objects.create_from_cart(cart)
add_order_to_request(request, order)
return order


2) Change my cart modifier to accept the new request parameter:

class AddTaxes(BaseCartModifier):
"""
Add sales tax to the cart, based on the user's state.
"""

def process_cart(self, cart, state={}):
"""
If we were passed a "request" state parameter, save it to
self.request for later use.

We need access to the request object so that we can determine
the user's address for
tax calculations.
"""
if state and state.has_key('request'):
self.request = state['request']
super(AddTaxes, self).process_cart(cart, state)

3) Elsewhere in my cart modifier, use
shop.util.address.get_shipping_address_from_request to get the user's
address from self.request.

The following discussion was helpful in pointing me to patching the
checkout view:
https://groups.google.com/group/django-shop/browse_thread/thread/60fd4297fc99ca65?pli=1

Hope that helps somebody!

Tribaal

unread,
Dec 21, 2011, 4:43:45 AM12/21/11
to djang...@googlegroups.com
Thanks for sharing :)

__underscore

unread,
Dec 21, 2011, 2:51:17 PM12/21/11
to django-shop
No problem, can you think of a cleaner way to do it? If not, would you
consider changing the built-in views to always pass the request object
in to the cart? It could make writing cart modifiers like this a lot
easier.

Thanks for a cool project!
> >https://groups.google.com/group/django-shop/browse_thread/thread/60fd...
Reply all
Reply to author
Forward
0 new messages