self::get_input_fields(), 'outputFields' => self::get_output_fields(), 'mutateAndGetPayload' => self::mutate_and_get_payload(), ] ); } /** * Defines the mutation input field configuration * * @return array */ public static function get_input_fields() { return [ 'productId' => [ 'type' => [ 'non_null' => 'Int' ], 'description' => static function () { return __( 'Cart item product database ID or global ID', 'graphql-for-ecommerce' ); }, ], 'quantity' => [ 'type' => 'Int', 'description' => static function () { return __( 'Cart item quantity', 'graphql-for-ecommerce' ); }, ], 'variationId' => [ 'type' => 'Int', 'description' => static function () { return __( 'Cart item product variation database ID or global ID', 'graphql-for-ecommerce' ); }, ], 'variation' => [ 'type' => [ 'list_of' => 'ProductAttributeInput' ], 'description' => static function () { return __( 'Cart item product variation attributes', 'graphql-for-ecommerce' ); }, ], 'extraData' => [ 'type' => 'String', 'description' => static function () { return __( 'JSON string representation of extra cart item data', 'graphql-for-ecommerce' ); }, ], ]; } /** * Defines the mutation output field configuration * * @return array */ public static function get_output_fields() { return [ 'cartItem' => [ 'type' => 'CartItem', 'resolve' => static function ( $payload ) { return \WC()->cart->get_cart_item( $payload['key'] ); }, ], 'cart' => Cart_Mutation::get_cart_field( true ), ]; } /** * Defines the mutation data modification closure. * * @return callable */ public static function mutate_and_get_payload() { return static function ( $input, AppContext $context, ResolveInfo $info ) { Cart_Mutation::check_session_token(); // Prepare args for "add_to_cart" from input data. $cart_item_args = Cart_Mutation::prepare_cart_item( $input, $context, $info ); // Add item to cart and get item key. try { $cart_item_key = \WC()->cart->add_to_cart( ...$cart_item_args ); } catch ( \Throwable $e ) { // Repackage any errors. throw new UserError( $e->getMessage() ); } // If cart item key valid return payload. if ( false !== $cart_item_key ) { return [ 'key' => $cart_item_key ]; } // Process errors. $notices = \WC()->session->get( 'wc_notices' ); if ( ! empty( $notices['error'] ) ) { $cart_error_messages = implode( ' ', array_column( $notices['error'], 'notice' ) ); \wc_clear_notices(); throw new UserError( $cart_error_messages ); } else { throw new UserError( __( 'Failed to add cart item. Please check input.', 'graphql-for-ecommerce' ) ); } }; } }