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.
Public Properties ​
Property | Description |
---|---|
activeCartEdgeDuration | string |
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
cart | craft\commerce\elements\Order |
cartCookie | array – The configuration of the cart cookie. |
cartCookieDuration | integer – The expiration duration of the cart cookie, in seconds. |
cartName | |
hasSessionCartNumber | boolean |
sessionCartNumber | string, mixed |
activeCartEdgeDuration
​
- Type
- string
- Default value
null
cart
​
- Type
- craft\commerce\elements\Order
- Default value
null
cartCookie
​
- Type
- array
- Default value
[]
- Since
- 4.0.0
The configuration of the cart cookie.
See also setSessionCartNumber()
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()
cartName
​
- Default value
null
- Access
- Read-only
- Since
- 3.1
hasSessionCartNumber
​
- Type
- boolean
- Default value
null
sessionCartNumber
​
- Type
- string,
mixed
- Default value
null
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() | 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.
generateCartNumber()
​
- Since
- 2.0
Generates a new random cart number and returns it.
getActiveCartEdgeDuration()
​
- Since
- 2.2
Calculates the date of the active cart duration edge.
Throws ​
getCart()
​
Get the current cart for this session.
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
getHasSessionCartNumber()
​
- Since
- 2.1.11
Returns whether there is a cart number in the session.
Throws ​
init()
​
Initializes the cart service
Returns ​
void
Throws ​
purgeIncompleteCarts()
​
Removes all carts that are incomplete and older than the config setting.
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.
Throws ​
- craft\errors\ElementNotFoundException
- yii\base\Exception
- craft\errors\MissingComponentException
- Throwable
setSessionCartNumber()
​
Set the session cart number.
Arguments ​
$cartNumber
Protected Methods ​
Method | Description |
---|---|
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.
loadCookie()
​
Returns ​
void