Skip to content

Commit

Permalink
Cognitive complexity of "mutate_and_get_payload" reduced.
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Aug 5, 2020
1 parent 4523c87 commit d79c730
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions includes/mutation/class-cart-add-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,18 @@ public static function mutate_and_get_payload( $input, AppContext $context, Reso
// Add item to cart and get item key.
$cart_item_key = \WC()->cart->add_to_cart( ...$cart_item_args );

if ( false === $cart_item_key ) {
$notices = \WC()->session->get( 'wc_notices' );
if ( ! empty( $notices['error'] ) ) {
$cart_error_messages = implode( ' ', array_column( $notices['error'], 'notice' ) );
throw new UserError( $cart_error_messages );
} else {
throw new UserError( __( 'Failed to add cart item. Please check input.', 'wp-graphql-woocommerce' ) );
}
// If cart item key valid return payload.
if ( false !== $cart_item_key ) {
return array( 'key' => $cart_item_key );
}

// Return payload.
return array( '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' ) );
throw new UserError( $cart_error_messages );
} else {
throw new UserError( __( 'Failed to add cart item. Please check input.', 'wp-graphql-woocommerce' ) );
}
}
}

0 comments on commit d79c730

Please sign in to comment.