Google Groups

Re: Accessing the user's address for a cart modifier

__underscore Dec 20, 2011 3:59 PM
Posted in group: 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

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
    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:

Hope that helps somebody!

On Dec 19, 5:54 pm, __underscore <> wrote:
> 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!