CartItemSchema::IDENTIFIER, 'namespace' => self::EXTENSION_NAMESPACE, 'data_callback' => array( self::class, 'cart_item_data' ), 'schema_callback' => array( self::class, 'cart_item_schema' ), 'schema_type' => ARRAY_A, ) ); woocommerce_store_api_register_endpoint_data( array( 'endpoint' => CartSchema::IDENTIFIER, 'namespace' => self::EXTENSION_NAMESPACE, 'data_callback' => array( self::class, 'cart_data' ), 'schema_callback' => array( self::class, 'cart_schema' ), 'schema_type' => ARRAY_A, ) ); } public static function cart_data(): array { if ( ! Connection::is_active() ) { return array(); } $cart = WC()->cart; // Mint an id only for a non-empty cart; an empty guest cart has no session to keep it. $cart_id = $cart instanceof WC_Cart && ! $cart->is_empty() ? CartQueue::cart_id() : CartQueue::current_cart_id(); return '' === $cart_id ? array() : array( 'cart_id' => $cart_id ); } public static function cart_schema(): array { return array( 'cart_id' => array( 'description' => 'Flowfy cart id, shared with classic cart events.', 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), ); } public static function cart_item_data( $cart_item ): array { $product = is_array( $cart_item ) && isset( $cart_item['data'] ) ? $cart_item['data'] : null; return $product instanceof WC_Product ? array( 'product_id' => Payloads::product_id( $product ) ) : array(); } public static function cart_item_schema(): array { return array( 'product_id' => array( 'description' => 'Flowfy product id: the parent product for a variation.', 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), ); } }