> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cocartapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Action hooks available in the core of CoCart

> Trigger an action when these hooks are called

Each action hook is documented below with its description and usage example.

<Warning>
  The action hooks listed here are not complete! Feel free to inquire about any specific action hook you may need help with.
</Warning>

## Installation Hooks

### `cocart_init`

Fires once CoCart has finished loading.

<Info>
  Made available since v3.0.0
</Info>

**Usage**

```php theme={"system"}
do_action('cocart_init', function() {
    // Your code here
});
```

**Defined in**: [`includes/class-cocart.php`](https://github.com/co-cart/co-cart/blob/trunk/includes/class-cocart.php#L142)

***

### `cocart_updated`

Runs after CoCart has been updated.

<Info>
  Made available since v1.2.0
</Info>

**Usage**

```php theme={"system"}
do_action('cocart_updated', function() {
    // Your code here
});
```

**Defined in**: [`includes/classes/class-cocart-install.php`](https://github.com/co-cart/co-cart/blob/trunk/includes/classes/class-cocart-install.php#L78)

***

### `cocart_installed`

Runs after CoCart has been installed.

<Info>
  Made available since v1.2.0
</Info>

**Usage**

```php theme={"system"}
do_action('cocart_installed', function() {
    // Your code here
});
```

**Defined in**: [`includes/classes/class-cocart-install.php`](https://github.com/co-cart/co-cart/blob/trunk/includes/classes/class-cocart-install.php#L256)

***

## Cart Updates

### `cocart_update_cart_before_totals`

Fires before the cart has updated via a callback, but before cart totals are re-calculated.

**Parameters**

<ParamField query="request" type="WP_REST_Request">
  The request object.
</ParamField>

<ParamField query="controller" type="object">
  The cart controller.
</ParamField>

<Info>
  Made available since v4.1.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_update_cart_before_totals', function($request, $controller) {
    // Your code here
}, 10, 2);
```

**Defined in**: [`includes/abstracts/abstract-cocart-extension-callback.php`](https://github.com/co-cart/co-cart/blob/trunk/includes/abstracts/abstract-cocart-extension-callback.php#L85)

***

### `cocart_update_cart_after_totals`

Fires after the cart has updated via a callback and cart totals are re-calculated.

**Parameters**

<ParamField query="request" type="WP_REST_Request">
  The request object.
</ParamField>

<ParamField query="controller" type="object">
  The cart controller.
</ParamField>

<Info>
  Made available since v4.1.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_update_cart_after_totals', function($request, $controller) {
    // Your code here
}, 10, 2);
```

***

## Session Hooks

### `cocart_after_session_saved_data`

Fires after session data is saved.

**Parameters**

<ParamField query="customer_id" type="int">
  Customer ID.
</ParamField>

<ParamField query="data" type="array">
  Cart data.
</ParamField>

<ParamField query="cart_expiration" type="int">
  Cart expiration.
</ParamField>

<ParamField query="cart_source" type="string">
  Cart source.
</ParamField>

<Info>
  Made available since v4.2.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_after_session_saved_data', function($customer_id, $data, $cart_expiration, $cart_source) {
    // Your code here
}, 10, 4);
```

***

### `cocart_cart_loaded`

Fires once a cart has loaded. Can be used to trigger a webhook.

**Parameters**

<ParamField query="cart_key" type="string">
  The cart key.
</ParamField>

<Info>
  Made available since v3.8.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_cart_loaded', function($cart_key) {
    // Your code here
}, 10, 1);
```

***

## Cart Items Management

### `cocart_item_added_updated_in_cart`

Fires if item was added again to the cart but updated the quantity.

**Parameters**

<ParamField query="cart_item_key" type="string">
  Item key of the item added again.
</ParamField>

<ParamField query="item_added" type="array">
  Item added to cart again.
</ParamField>

<ParamField query="new_quantity" type="int">
  New quantity of the item.
</ParamField>

<Info>
  Made available since v2.1.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_item_added_updated_in_cart', function($cart_item_key, $item_added, $new_quantity) {
    // Your code here
}, 10, 3);
```

***

### `cocart_item_added_to_cart`

Fires once an item has been added to cart.

**Parameters**

<ParamField query="item_key" type="string">
  Item key of the item added.
</ParamField>

<ParamField query="item_added" type="array">
  Item added to cart.
</ParamField>

<Info>
  Made available since v2.1.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_item_added_to_cart', function($item_key, $item_added) {
    // Your code here
}, 10, 2);
```

***

### `cocart_item_removed`

Fires when an item is removed from the cart.

**Parameters**

<ParamField query="current_data" type="array">
  The cart item data.
</ParamField>

<Info>
  Made available since v2.0.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_item_removed', function($current_data) {
    // Your code here
}, 10, 1);
```

***

### `cocart_item_restored`

Fires when an item is restored to the cart.

**Parameters**

<ParamField query="current_data" type="array">
  The cart item data.
</ParamField>

<Info>
  Made available since v2.0.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_item_restored', function($current_data) {
    // Your code here
}, 10, 1);
```

***

### `cocart_item_quantity_changed`

Fires when the quantity of an item in the cart is changed.

**Parameters**

<ParamField query="item_key" type="string">
  Item key.
</ParamField>

<ParamField query="new_data" type="array">
  Item data.
</ParamField>

<Info>
  Made available since v2.0.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_item_quantity_changed', function($item_key, $new_data) {
    // Your code here
}, 10, 2);
```

***

## Product Reviews

### `cocart_insert_product_review`

Fires after a comment is created via the REST API.

**Parameters**

<ParamField query="review" type="WP_Comment">
  Inserted comment object.
</ParamField>

<ParamField query="request" type="WP_REST_Request">
  The request object.
</ParamField>

<ParamField query="creating" type="bool">
  True when creating a comment, false when updating.
</ParamField>

<Info>
  Made available since v2.1.0
</Info>

**Usage**

```php theme={"system"}
add_action('cocart_insert_product_review', function($review, $request, $creating) {
    // Your code here
}, 10, 3);
```

***

## Deprecated Hooks

### `cocart_cart`

<Warning>**Deprecated:** 3.0.0 Use `cocart_cart` hook instead.</Warning>

**Usage**

```php theme={"system"}
do_action('cocart_cart', function() {
    // Your code here
});
```

***

### `cocart_update_cart_before_totals`

<Warning>**Deprecated:** 4.1.0 Replaced with `cocart_update_cart_before_totals` hook.</Warning>

**Usage**

```php theme={"system"}
do_action('cocart_update_cart_before_totals', function() {
    // Your code here
});
```
