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 ); } }