Skip to content

Commit

Permalink
Shipping offers with zero discount ae not included
Browse files Browse the repository at this point in the history
See issue #745
  • Loading branch information
Izidor Matušov committed Jul 16, 2013
1 parent a2906fd commit c42818a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
5 changes: 5 additions & 0 deletions oscar/apps/order/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from oscar.apps.order.exceptions import UnableToPlaceOrder
from oscar.core.loading import get_class

from decimal import Decimal as D

ShippingAddress = get_model('order', 'ShippingAddress')
Order = get_model('order', 'Order')
Line = get_model('order', 'Line')
Expand Down Expand Up @@ -78,6 +80,9 @@ def place_order(self, basket, total_incl_tax=None, total_excl_tax=None,
application['message'] = application['offer'].apply_deferred_benefit(basket)
# Record offer application results
if application['result'].affects_shipping:
# Skip zero shipping discounts
if shipping_method.discount <= D('0.00'):
continue
# If a shipping offer, we need to grab the actual discount off
# the shipping method instance, which should be wrapped in an
# OfferDiscount instance.
Expand Down
5 changes: 4 additions & 1 deletion oscar/apps/shipping/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from oscar.apps.shipping.methods import (
Free, NoShippingRequired, OfferDiscount)

from decimal import Decimal as D


class Repository(object):
"""
Expand Down Expand Up @@ -59,7 +61,8 @@ def prime_method(self, basket, method):
if basket.offer_applications.shipping_discounts:
# We assume there is only one shipping discount available
discount = basket.offer_applications.shipping_discounts[0]
return OfferDiscount(method, discount['offer'])
if method.basket_charge_incl_tax > D('0.00'):
return OfferDiscount(method, discount['offer'])
return method

def find_by_code(self, code, basket):
Expand Down
65 changes: 63 additions & 2 deletions tests/integration/order/creator_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
from mock import Mock

from oscar.apps.basket.models import Basket
from oscar.apps.catalogue.models import ProductClass, Product
from oscar.apps.offer.utils import Applicator
from oscar.apps.order.models import Order
from oscar.test.factories import create_product
from oscar.apps.order.utils import OrderCreator
from oscar.apps.catalogue.models import ProductClass, Product
from oscar.apps.shipping.methods import FixedPrice, Free
from oscar.apps.shipping.repository import Repository
from oscar.core.loading import get_class
from oscar.test.factories import create_product, create_offer

Range = get_class('offer.models', 'Range')
Benefit = get_class('offer.models', 'Benefit')


class TestOrderCreatorErrorCases(TestCase):
Expand Down Expand Up @@ -105,3 +112,57 @@ def test_does_not_allocate_stock(self):
product_ = Product.objects.get(id=product.id)
self.assertTrue(product_.stockrecord.num_in_stock is None)
self.assertTrue(product_.stockrecord.num_allocated is None)

class StubRepository(Repository):
""" Custom shipping methods """
methods = (FixedPrice(D('5.00')), Free())

class TestShippingOfferForOrder(TestCase):

def setUp(self):
self.creator = OrderCreator()
self.basket = Basket.objects.create()

def apply_20percent_shipping_offer(self):
"""Shipping offer 20% off"""
range = Range.objects.create(name="All products range",
includes_all_products=True)
benefit = Benefit.objects.create(
range=range, type=Benefit.SHIPPING_PERCENTAGE, value=20)
offer = create_offer(range=range, benefit=benefit)
Applicator().apply_offers(self.basket, [offer])

def test_shipping_offer_is_applied(self):
self.basket.add_product(create_product(price=D('12.00')))
self.apply_20percent_shipping_offer()

# Normal shipping 5.00
shipping = StubRepository().find_by_code(FixedPrice.code, self.basket)

self.creator.place_order(
basket=self.basket,
order_number='1234',
shipping_method=shipping)
order = Order.objects.get(number='1234')

self.assertEqual(1, len(order.shipping_discounts))
self.assertEqual(D('4.00'), order.shipping_incl_tax)
self.assertEqual(D('16.00'), order.total_incl_tax)

def test_zero_shipping_discount_is_not_created(self):
self.basket.add_product(create_product(price=D('12.00')))
self.apply_20percent_shipping_offer()

# Free shipping
shipping = StubRepository().find_by_code(Free.code, self.basket)

self.creator.place_order(
basket=self.basket,
order_number='1234',
shipping_method=shipping)
order = Order.objects.get(number='1234')

# No shipping discount
self.assertEqual(0, len(order.shipping_discounts))
self.assertEqual(D('0.00'), order.shipping_incl_tax)
self.assertEqual(D('12.00'), order.total_incl_tax)

0 comments on commit c42818a

Please sign in to comment.