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.

basket_quantity(line)[source]

Return the quantity of similar lines in the basket. The basket can contain multiple lines with the same product and stockrecord, but different options. Those quantities are summed up.

property can_be_edited

Test if a basket can be edited

contains_voucher(code)[source]

Test whether the basket contains a voucher with a given code

flush()[source]

Remove all lines from basket.

freeze()[source]

Freezes the basket so it cannot be modified.

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, line=None)[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.

If the basket is empty, then tax values are unknown.

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.

reset_offer_applications()[source]

Remove any discounts so they get recalculated

set_as_submitted()

Mark this basket as submitted

property shipping_discounts

Return discounts from vouchers

submit()[source]

Mark this basket as submitted

thaw()[source]

Unfreezes a basket so it can be modified again

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:

  1. 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.

  2. 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.

clear_discount()[source]

Remove any discounts from this line.

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

class oscar.apps.basket.abstract_models.AbstractLineAttribute(*args, **kwargs)[source]

An attribute of a basket line

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 AddToBasketForm

form_invalid(form)[source]

If the form is invalid, render the invalid form.

form_valid(form)[source]

If the form is valid, redirect to the supplied URL.

get_form_kwargs()[source]

Return the keyword arguments for instantiating the form.

get_success_url()[source]

Return the URL to redirect to after processing a valid form.

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 Product

class oscar.apps.basket.views.BasketView(**kwargs)[source]
basket_model

alias of Basket

form_class

alias of BasketLineForm

formset_class

alias of LineFormFormSet

formset_invalid(formset)[source]

If the formset is invalid, re-render the context data with the data-filled formset and errors.

formset_valid(formset)[source]

If the formset is valid, save the associated models.

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.

get_basket_warnings(basket)[source]

Return a list of warnings that apply to this basket

get_context_data(**kwargs)[source]

Get the context for this view.

get_formset_kwargs()[source]

Returns the keyword arguments for instantiating the formset.

get_queryset()[source]

Return list of Line instances associated with the current basket.

get_success_url()[source]

Returns the supplied URL.

model

alias of 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 Basket

form_class

alias of SavedLineForm

formset_class

alias of LineFormFormSet

formset_invalid(formset)[source]

If the formset is invalid, re-render the context data with the data-filled formset and errors.

formset_valid(formset)[source]

If the formset is valid, save the associated models.

get(request, *args, **kwargs)[source]

Handles GET requests and instantiates a blank version of the formset.

get_formset_kwargs()[source]

Returns the keyword arguments for instantiating the formset.

get_queryset()[source]

Return list of Line instances associated with the saved basked associated with the currently authenticated user.

get_success_url()[source]

Returns the supplied URL.

model

alias of Line

class oscar.apps.basket.views.VoucherAddView(**kwargs)[source]
form_class

alias of BasketVoucherForm

form_invalid(form)[source]

If the form is invalid, render the invalid form.

form_valid(form)[source]

If the form is valid, redirect to the supplied URL.

get(request, *args, **kwargs)[source]

Handle GET requests: instantiate a blank version of the form.

voucher_model

alias of Voucher

class oscar.apps.basket.views.VoucherRemoveView(**kwargs)[source]
voucher_model

alias of Voucher