Carts

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\Carts » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
2.0

Cart service. This manages the cart currently in the session, this service should mainly be used by web controller actions.

View source

# Public Properties

# activeCartEdgeDuration

Type
string
Access
Read-only
Since
2.2

View source

# cart

Type
craft\commerce\elements\Order
Access
Read-only

View source

# hasSessionCartNumber

Type
boolean
Access
Read-only
Since
2.1.11

View source

# Protected Properties

Property Description
cartName string – Session key for storing the cart number

# cartName

Type
string

Session key for storing the cart number

View source

# Public Methods

Method Description
__call() Calls the named method which is not a class method.
__clone() This method is called after the object is created by cloning an existing one.
__construct() Constructor.
__get() Returns the value of a component property.
__isset() Checks if a property is set, i.e. defined and not null.
__set() Sets the value of a component property.
__unset() Sets a component property to be null.
attachBehavior() Attaches a behavior to this component.
attachBehaviors() Attaches a list of behaviors to the component.
behaviors() Returns a list of behaviors that this component should behave as.
canGetProperty() Returns a value indicating whether a property can be read.
canSetProperty() Returns a value indicating whether a property can be set.
className() Returns the fully qualified name of this class.
detachBehavior() Detaches a behavior from the component.
detachBehaviors() Detaches all behaviors from the component.
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component.
forgetCart() Forgets the cart in the current session.
generateCartNumber() Generate a new random cart number and returns it.
getActiveCartEdgeDuration() Calculates the date of the active cart duration edge.
getBehavior() Returns the named behavior object.
getBehaviors() Returns all behaviors attached to this component.
getCart() Get the current cart for this session.
getCartName()
getHasSessionCartNumber() Returns whether there is a cart number in the session.
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event.
hasMethod() Returns a value indicating whether a method is defined.
hasProperty() Returns a value indicating whether a property is defined for this component.
init() Initializes the object.
off() Detaches an existing event handler from this component.
on() Attaches an event handler to an event.
purgeIncompleteCarts() Removes all carts that are incomplete and older than the config setting.
restorePreviousCartForCurrentUser() Restores previous cart for the current user if their current cart is empty.
trigger() Triggers an event.

# forgetCart()

Forgets the cart in the current session.

View source

Returns

void

Throws

# generateCartNumber()

Since
2.0

Generate a new random cart number and returns it.

View source

Returns

string

# getActiveCartEdgeDuration()

Since
2.2

Calculates the date of the active cart duration edge.

View source

Returns

string

Throws

# getCart()

Get the current cart for this session.

View source

Arguments

  • $forceSave (boolean) – Force the cart to save when requesting it.

Returns

craft\commerce\elements\Order

Throws

# getCartName()

Since
3.1

View source

Returns

string

# getHasSessionCartNumber()

Since
2.1.11

Returns whether there is a cart number in the session.

View source

Returns

boolean

Throws

# purgeIncompleteCarts()

Removes all carts that are incomplete and older than the config setting.

View source

Returns

integer – The number of carts purged from the database

Throws

# restorePreviousCartForCurrentUser()

Restores previous cart for the current user if their current cart is empty.

Ideally this is only used when a user logs in.

View source

Throws