# booktics/1.0.3/core/cart/cart-session.php

Booktics – Appointment Booking Calendar for Service Businesses, version 1.0.3. 129 lines.

- Page: https://pluginprobe.com/plugins/booktics/1.0.3/code/core/cart/cart-session.php
- Raw: https://pluginprobe.com/plugins/booktics/1.0.3/raw/core/cart/cart-session.php
- Modified: 2025-07-10T13:13:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/booktics/1.0.3/code/core/cart/cart-session.php#L10-L20`.

```php
<?php

namespace Booktics\Cart;

/**
 * Class Cart_Session
 * Handles cart session management for the Booktics plugin
 *
 * @package Booktics\Cart
 */
class Cart_Session {

    /**
     * Session key used to store cart data in a PHP session
     *
     * @var string
     */
    const SESSION_KEY = 'booktics_cart';

    /**
     * Get cart data from session
     *
     * @return array{items: array, total: float}
     */
    public static function get() {
        $session_data = ! empty( $_SESSION[ self::SESSION_KEY ] ) ? $_SESSION[ self::SESSION_KEY ] : null;

        if ( ! is_array( $session_data ) ) {
            return array(
                'items'  => array(),
                'total'  => 0.00,
                'coupon' => '',
            );
        }

        return $session_data;
    }

    /**
     * Set cart data in session
     *
     * @param array $cart Cart data to store in session
     *
     * @return void
     */
    public static function set( array $cart ) {
        $_SESSION[ self::SESSION_KEY ] = $cart;
    }

    /**
     * Clear cart data from session
     *
     * @return void
     */
    public static function clear() {
        unset( $_SESSION[ self::SESSION_KEY ] );
    }

    /**
     * Add an item to the cart session
     *
     * @param array $item
     *
     * @return array
     */
    public static function add_item( array $item ): array {
        $cart            = self::get();
        $cart['items'][] = $item;
        $cart['total']   += $item['subtotal'];
        $cart            = apply_filters( 'booktics_pro_cart_session', $cart );

        self::set( $cart );

        return end( $cart['items'] );
    }

    /**
     * Delete an item from the cart session
     *
     * @param string $id
     *
     * @return void
     */
    public static function delete_item( string $id ) {
        $cart          = self::get();
        $cart['items'] = array_values(
            array_filter(
                $cart['items'],
                function ( $item ) use ( $id ) {
                    return $item['uuid'] != $id;
                }
            )
        );
        $cart['total'] = array_reduce(
            $cart['items'], function ( $total, $item ) {
				return $total + $item['price'];
			}, 0.00
        );

        self::set( $cart );
    }

    /**
     * Edit an item in the cart session
     *
     * @param string $uuid UUID of the item to edit
     * @param array $new_data New data to update the item with
     *
     * @return void
     */
    public static function edit_item( string $uuid, array $new_data ) {
        $cart          = self::get();
        $cart['total'] = 0;

        $cart['items'] = array_map(
            function ( $item ) use ( $uuid, $new_data, &$cart ) {
                if ( $item['uuid'] === $uuid ) {
                        $item = array_merge( $item, $new_data );
                }
                $cart['total'] += $item['subtotal'];

                return $item;
            }, $cart['items']
        );
        $cart          = apply_filters( 'booktics_pro_cart_session', $cart );
        self::set( $cart );
    }
}

```
