Shopping Carts, Orders, and Line Items

Drupal
    Commerce Shopping Cart
  • Click "Add to Cart"
  • Click "View Cart"
  • Arrive at /cart

Quick! What's the difference between a shopping cart, an order, and a line item? The answer? Not much. A shopping cart is just a kind of an order and an order is just a revisioned group of line items. How about we try that again with less Commerce-jargon?

  • Shopping Cart – Also sometimes called a basket, it's the listing of products your customers wants to buy at checkout.
  • Order – An order is a list of products with a status. In our case, a shopping cart is simply an order with a status of "Shopping Cart."
  • Line Item – Every product on the order is referenced by a record that includes quantity and a reference to which order it belongs. This record is known as a line item. There are other kinds of line items (like taxes, or discounts) but those don't really play that much of a role in a shopping cart.

Shopping Cart Overview

Order
        Status for Shopping Carts

Order with Status

A shopping cart in Drupal Commerce is simply an order with a particular cart order status. In fact, almost everything in Drupal Commerce revolves around changing the order status.

  • Administration
  • Store
  • View Orders
  • Add / Edit Order
  • Order Status at Bottom

As soon as a product is added to the cart, an order is created and associated with the customer either via user ID if logged in or via an array in the session if not (commerce_cart_orders).

The shopping cart is totally optional, meaning checkout is implemented in a separate module and will still function properly for orders created by the administrator. This allows for sites to do invoicing and payment collection where users shouldn't be allowed to create and manage their own orders via a cart.

Administrative
        View for Shopping Carts

Administrative View

When you view all of the open shopping carts, it is possible that one customer has multiple open shopping carts.

  • Administration
  • Store
  • View Orders
  • Click "Shopping Carts" in the upper right.

Each change to a shopping cart order is saved as a revision. A user can also have more than one cart, though the default is to present the most recent shopping cart to the user. hook_commerce_cart_order_id() can be used to introduce alternate logic to the cart load process if necessary.

Note: As long as the order is still in the shopping cart state, its line items will be re-validated on load against the latest product prices / availability, etc.

Modifying a Line Item in Cart Using Rules

This is an advanced action that involves Rules and setting values, but we thought it was worth documenting seeing as how we've gotten lots of issues and requests regarding this kind of functionality. For example, lets say you can only buy Product A if Product B is not in the cart. You can make Drupal Commerce understand this using Rules. We describe the recommended rule below.

Clone
        the unset price rule to set up your own rule with your own logic

Unset Price Rule

To remove an item from the cart using rules, you must unset the price of the line item during "Calculating the sell price of a product." Deleting a line item just involves leaving the price amount empty.

  • Administration
  • Store
  • Configuration
  • Produt Pricing Rules
  • Click "Clone"

To remove an item from the cart using rules, you must unset the price of the line item during "Calculating the sell price of a product." This is similar to the rule "Unset the price of disabled products in the cart" that is included with core. So you can refer to that as an example. Deleting a line item just involves setting the price to NULL (i.e. no value, not 0).

Thanks to ressa over at Drupal.org, we know we need to further clarify our advice: "For those trying to remove a line item by entering an empty field, and setting the price to NULL, you do it under 'Add action' -> 'Data' -> 'Set a data value'. Under Data Selector, enter 'commerce-line-item:commerce-unit-price:amount' ... and just leave the 'Value' field empty, don't actually type "NULL" :-)"

Related Assets

Are you looking for how to modify the cart block? Checkout our entire article on modifying the Shopping Cart block using views.

Found errors? Think you can improve this documentation? edit this page