Skip to content

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 ​

PropertyDescription
activeCartEdgeDurationstring
behaviorsyii\base\Behavior – List of behaviors attached to this component.
cartcraft\commerce\elements\Order
cartCookiearray – The configuration of the cart cookie.
cartCookieDurationinteger – The expiration duration of the cart cookie, in seconds.
cartName
hasSessionCartNumberboolean
sessionCartNumberstring, mixed

activeCartEdgeDuration ​

Type
string
Default value
null

View source

cart ​

Type
craft\commerce\elements\Order
Default value
null

View source

cartCookie ​

Type
array
Default value
[]
Since
4.0.0

The configuration of the cart cookie.

See also setSessionCartNumber()

View source

cartCookieDuration ​

Type
integer
Default value
31536000
Since
4.0.0

The expiration duration of the cart cookie, in seconds. (Defaults to one year.)

See also setSessionCartNumber()

View source

cartName ​

Default value
null
Access
Read-only
Since
3.1

View source

hasSessionCartNumber ​

Type
boolean
Default value
null

View source

sessionCartNumber ​

Type
string, mixed
Default value
null

View source

Public Methods ​

MethodDescription
__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()Generates 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 cart service
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.
setSessionCartNumber()Set the session cart number.
trigger()Triggers an event.

forgetCart() ​

Forgets the cart in the current session.

View source

generateCartNumber() ​

Since
2.0

Generates a new random cart number and returns it.

View source

getActiveCartEdgeDuration() ​

Since
2.2

Calculates the date of the active cart duration edge.

View source

Throws ​

getCart() ​

Get the current cart for this session.

View source

Arguments ​

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

Throws ​

getCartName() ​

DEPRECATED

Deprecated in 4.0.0. The cookie name is available via cartCookie ['name'].

Since
3.1

View source

getHasSessionCartNumber() ​

Since
2.1.11

Returns whether there is a cart number in the session.

View source

Throws ​

init() ​

Initializes the cart service

View source

Returns ​

void

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 ​

setSessionCartNumber() ​

Set the session cart number.

View source

Arguments ​

  • $cartNumber

Protected Methods ​

MethodDescription
getSessionCartNumber()Get the session cart number or generates one if none exists.
loadCookie()

getSessionCartNumber() ​

Get the session cart number or generates one if none exists.

View source

loadCookie() ​

View source

Returns ​

void

Throws ​