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 [ 'codes' => [ 'type' => [ 'list_of' => 'String' ], 'description' => static function () { return __( 'Code of coupon being applied', 'graphql-for-ecommerce' ); }, ], ]; } /** * Defines the mutation output field configuration * * @return array */ public static function get_output_fields() { return [ 'cart' => Cart_Mutation::get_cart_field(), ]; } /** * Defines the mutation data modification closure. * * @return callable */ public static function mutate_and_get_payload() { return static function ( $input ) { Cart_Mutation::check_session_token(); // Retrieve product database ID if relay ID provided. if ( empty( $input['codes'] ) ) { throw new UserError( __( 'No coupon codes provided', 'graphql-for-ecommerce' ) ); } foreach ( $input['codes'] as $code ) { // Check if applied. if ( ! \WC()->cart->has_discount( $code ) ) { throw new UserError( __( 'This coupon has not been applied to the cart.', 'graphql-for-ecommerce' ) ); } // Get cart item for payload. $success = \WC()->cart->remove_coupon( $code ); if ( true !== $success ) { throw new UserError( __( 'Failed to remove coupon.', 'graphql-for-ecommerce' ) ); } } // Recalculate totals after coupon removal. \WC()->cart->calculate_totals(); do_action( 'woographql_update_session', true ); // Return payload. return [ 'cart' => \WC()->cart ]; }; } }