Basket¶
The basket app handles shopping baskets, which essentially are a collection of products that hopefully end up being ordered.
Abstract models¶
-
class
oscar.apps.basket.abstract_models.
AbstractBasket
(*args, **kwargs)[source]¶ Basket object
-
add
(product, quantity=1, options=None)¶ Add a product to the basket
The ‘options’ list should contains dicts with keys ‘option’ and ‘value’ which link the relevant product.Option model and string value respectively.
- Returns (line, created).
line: the matching basket line created: whether the line was created or updated
-
add_product
(product, quantity=1, options=None)[source]¶ Add a product to the basket
The ‘options’ list should contains dicts with keys ‘option’ and ‘value’ which link the relevant product.Option model and string value respectively.
- Returns (line, created).
line: the matching basket line created: whether the line was created or updated
-
all_lines
()[source]¶ Return a cached set of basket lines.
This is important for offers as they alter the line models and you don’t want to reload them from the DB as that information would be lost.
-
applied_offers
()[source]¶ Return a dict of offers successfully applied to the basket.
This is used to compare offers before and after a basket change to see if there is a difference.
-
property
can_be_edited
¶ Test if a basket can be edited
-
get_stock_info
(product, options)[source]¶ Hook for implementing strategies that depend on product options
-
property
grouped_voucher_discounts
¶ Return discounts from vouchers but grouped so that a voucher which links to multiple offers is aggregated into one object.
-
property
is_empty
¶ Test if this basket is empty
-
is_quantity_allowed
(qty)[source]¶ Test whether the passed quantity of items can be added to the basket
-
is_shipping_required
()[source]¶ Test whether the basket contains physical products that require shipping.
-
property
is_tax_known
¶ Test if tax values are known for this basket
-
line_quantity
(product, stockrecord, options=None)[source]¶ Return the current quantity of a specific product and options
-
max_allowed_quantity
()[source]¶ Returns maximum product quantity, that can be added to the basket with the respect to basket quantity threshold.
-
merge
(basket, add_quantities=True)[source]¶ Merges another basket with this one.
- Basket
The basket to merge into this one.
- Add_quantities
Whether to add line quantities when they are merged.
-
merge_line
(line, add_quantities=True)[source]¶ For transferring a line from another basket to this one.
This is used with the “Saved” basket functionality.
-
property
num_items
¶ Return number of items
-
property
num_lines
¶ Return number of lines
-
property
offer_discounts
¶ Return basket discounts from non-voucher sources. Does not include shipping discounts.
-
property
post_order_actions
¶ Return discounts from vouchers
-
product_quantity
(product)[source]¶ Return the quantity of a product in the basket
The basket can contain multiple lines with the same product, but different options and stockrecords. Those quantities are summed up.
-
set_as_submitted
()¶ Mark this basket as submitted
-
property
shipping_discounts
¶ Return discounts from vouchers
-
property
total_excl_tax
¶ Return total line price excluding tax
-
property
total_excl_tax_excl_discounts
¶ Return total price excluding tax and discounts
-
property
total_incl_tax
¶ Return total price inclusive of tax and discounts
-
property
total_incl_tax_excl_discounts
¶ Return total price inclusive of tax but exclusive discounts
-
property
total_tax
¶ Return total tax for a line
-
property
voucher_discounts
¶ Return discounts from vouchers
-
-
class
oscar.apps.basket.abstract_models.
AbstractLine
(*args, **kwargs)[source]¶ A line of a basket (product and a quantity)
Common approaches on ordering basket lines:
First added at top. That’s the history-like approach; new items are added to the bottom of the list. Changing quantities doesn’t impact position. Oscar does this by default. It just sorts by Line.pk, which is guaranteed to increment after each creation.
Last modified at top. That means items move to the top when you add another one, and new items are added to the top as well. Amazon mostly does this, but doesn’t change the position when you update the quantity in the basket view. To get this behaviour, change Meta.ordering and optionally do something similar on wishlist lines. Order lines should already be created in the order of the basket lines, and are sorted by their primary key, so no changes should be necessary there.
-
consume
(quantity, offer=None)[source]¶ Mark all or part of the line as ‘consumed’
Consumed items are no longer available to be used in offers.
-
discount
(discount_value, affected_quantity, incl_tax=True, offer=None)[source]¶ Apply a discount to this line
-
get_price_breakdown
()[source]¶ Return a breakdown of line prices after discounts have been applied.
Returns a list of (unit_price_incl_tax, unit_price_excl_tax, quantity) tuples.
-
get_warning
()[source]¶ Return a warning message about this basket line if one is applicable
This could be things like the price has changed
-
property
purchase_info
¶ Return the stock/price info
-
save
(*args, **kwargs)[source]¶ Save the current instance. Override this in a subclass if you want to control the saving process.
The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.
-
property
unit_effective_price
¶ The price to use for offer calculations
Views¶
-
class
oscar.apps.basket.views.
BasketAddView
(**kwargs)[source]¶ Handles the add-to-basket submissions, which are triggered from various parts of the site. The add-to-basket form is loaded into templates using a templatetag from
oscar.templatetags.basket_tags
.-
form_class
¶ alias of
oscar.apps.basket.forms.AddToBasketForm
-
post
(request, *args, **kwargs)[source]¶ Handle POST requests: instantiate a form instance with the passed POST variables and then check if it’s valid.
-
product_model
¶ alias of
oscar.apps.catalogue.models.Product
-
-
class
oscar.apps.basket.views.
BasketView
(**kwargs)[source]¶ -
basket_model
¶ alias of
oscar.apps.basket.models.Basket
-
form_class
¶ alias of
oscar.apps.basket.forms.BasketLineForm
-
formset_class
¶ alias of
django.forms.formsets.LineFormFormSet
-
formset_invalid
(formset)[source]¶ If the formset is invalid, re-render the context data with the data-filled formset and errors.
-
get_basket_voucher_form
()[source]¶ This is a separate method so that it’s easy to e.g. not return a form if there are no vouchers available.
-
model
¶ alias of
oscar.apps.basket.models.Line
-
remove_deleted_forms
(formset)[source]¶ Removes forms marked for deletion, from the formset, as well as deletes their model instance objects; and modifies the formset’s request data, to match the state of the data in the database, for the remaining forms.
This is useful for redisplaying a formset containing other invalid forms, after deleting one of the forms from it.
-
-
class
oscar.apps.basket.views.
SavedView
(**kwargs)[source]¶ -
basket_model
¶ alias of
oscar.apps.basket.models.Basket
-
form_class
¶ alias of
oscar.apps.basket.forms.SavedLineForm
-
formset_class
¶ alias of
django.forms.formsets.LineFormFormSet
-
formset_invalid
(formset)[source]¶ If the formset is invalid, re-render the context data with the data-filled formset and errors.
-
get
(request, *args, **kwargs)[source]¶ Handles GET requests and instantiates a blank version of the formset.
-
get_queryset
()[source]¶ Return list of
Line
instances associated with the saved basked associated with the currently authenticated user.
-
model
¶ alias of
oscar.apps.basket.models.Line
-