How to create a custom offer condition¶
Oscar ships with several condition models that can be used to build offers. However, occasionally a custom condition can be useful. Oscar lets you build a custom condition class and register it so that it is available for building offers.
Custom condition interface¶
Custom condition classes must be proxy models, subclassing Oscar’s main
At a minimum, a custom condition must:
- have a
nameattribute which with the condition name.
- have a
descriptionattribute which describes what needs to happen to satisfy the condition (eg “basket must have 4 items”).
- have an
is_satisfiedmethod that takes a basket instance and an offer instance and returns a boolean indicating if the condition is satisfied
It can also implement:
can_apply_conditionmethod that takes a product instance and returns a boolean depending on whether the condition is applicable to the product.
consume_itemsmethod that marks basket items as consumed once the condition has been met.
get_upsell_messagemethod that returns a message for the customer, letting them know what they would need to do to qualify for this offer.
is_partially_satisfiedmethod that tests to see if the customer’s basket partially satisfies the condition (ie when you might want to show them an upsell message)
from oscar.apps.offer import models class BasketOwnerCalledBarry(models.Condition): name = "User must be called barry" class Meta: proxy = True def is_satisfied(self, offer, basket): if not basket.owner: return False return basket.owner.first_name.lower() == 'barry'
Create condition instance¶
To make this condition available to be used in offers, do the following:
from oscar.apps.offer.custom import create_condition create_condition(BasketOwnerCalledBarry)
Now you should see this condition in the dashboard when creating/updating an offer.