add ability to inspect a cart for the existence of a particular type of item, e.g. PaidCourseRegistration. Only show the 'Shopping Cart' button if there's a PaidCourseRegistration in it.
This commit is contained in:
@@ -19,5 +19,8 @@ def user_has_cart_context_processor(request):
|
||||
request.user.is_authenticated() and # user is logged in and
|
||||
settings.FEATURES.get('ENABLE_PAID_COURSE_REGISTRATION') and # settings enable paid course reg and
|
||||
settings.FEATURES.get('ENABLE_SHOPPING_CART') and # settings enable shopping cart and
|
||||
shoppingcart.models.Order.user_cart_has_items(request.user) # user's cart has items
|
||||
shoppingcart.models.Order.user_cart_has_items(
|
||||
request.user,
|
||||
shoppingcart.models.PaidCourseRegistration
|
||||
) # user's cart has PaidCourseRegistrations
|
||||
)}
|
||||
|
||||
@@ -87,15 +87,17 @@ class Order(models.Model):
|
||||
return cart_order
|
||||
|
||||
@classmethod
|
||||
def user_cart_has_items(cls, user):
|
||||
def user_cart_has_items(cls, user, item_type=None):
|
||||
"""
|
||||
Returns true if the user (anonymous user ok) has
|
||||
a cart with items in it. (Which means it should be displayed.
|
||||
If a item_type is passed in, then we check to see if the cart has at least one of
|
||||
those types of OrderItems
|
||||
"""
|
||||
if not user.is_authenticated():
|
||||
return False
|
||||
cart = cls.get_cart_for_user(user)
|
||||
return cart.has_items()
|
||||
return cart.has_items(item_type)
|
||||
|
||||
@property
|
||||
def total_cost(self):
|
||||
@@ -105,11 +107,19 @@ class Order(models.Model):
|
||||
"""
|
||||
return sum(i.line_cost for i in self.orderitem_set.filter(status=self.status)) # pylint: disable=E1101
|
||||
|
||||
def has_items(self):
|
||||
def has_items(self, item_type=None):
|
||||
"""
|
||||
Does the cart have any items in it?
|
||||
If an item_type is passed in then we check to see if there are any items of that class type
|
||||
"""
|
||||
return self.orderitem_set.exists() # pylint: disable=E1101
|
||||
if not item_type:
|
||||
return self.orderitem_set.exists() # pylint: disable=E1101
|
||||
else:
|
||||
items = self.orderitem_set.all().select_subclasses()
|
||||
for item in items:
|
||||
if isinstance(item, item_type):
|
||||
return True
|
||||
return False
|
||||
|
||||
def clear(self):
|
||||
"""
|
||||
|
||||
@@ -52,6 +52,21 @@ class OrderTest(ModuleStoreTestCase):
|
||||
item = OrderItem(order=cart, user=self.user)
|
||||
item.save()
|
||||
self.assertTrue(Order.user_cart_has_items(self.user))
|
||||
self.assertFalse(Order.user_cart_has_items(self.user, CertificateItem))
|
||||
self.assertFalse(Order.user_cart_has_items(self.user, PaidCourseRegistration))
|
||||
|
||||
def test_user_cart_has_paid_course_registration_items(self):
|
||||
cart = Order.get_cart_for_user(self.user)
|
||||
item = PaidCourseRegistration(order=cart, user=self.user)
|
||||
item.save()
|
||||
self.assertTrue(Order.user_cart_has_items(self.user, PaidCourseRegistration))
|
||||
self.assertFalse(Order.user_cart_has_items(self.user, CertificateItem))
|
||||
|
||||
def test_user_cart_has_certificate_items(self):
|
||||
cart = Order.get_cart_for_user(self.user)
|
||||
CertificateItem.add_to_order(cart, self.course_key, self.cost, 'honor')
|
||||
self.assertTrue(Order.user_cart_has_items(self.user, CertificateItem))
|
||||
self.assertFalse(Order.user_cart_has_items(self.user, PaidCourseRegistration))
|
||||
|
||||
def test_cart_clear(self):
|
||||
cart = Order.get_cart_for_user(user=self.user)
|
||||
|
||||
Reference in New Issue
Block a user