mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
Merge pull request #312 from kidunot89/feature/better-add-to-cart-error-handling
New error handling method introduced in the "addToCart" mutation
This commit is contained in:
@@ -107,8 +107,14 @@ class Cart_Add_Item {
|
||||
// Add item to cart and get item key.
|
||||
$cart_item_key = \WC()->cart->add_to_cart( ...$cart_item_args );
|
||||
|
||||
if ( empty( $cart_item_key ) ) {
|
||||
throw new UserError( __( 'Failed to add cart item. Please check input.', 'wp-graphql-woocommerce' ) );
|
||||
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' ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Return payload.
|
||||
|
||||
@@ -23,7 +23,17 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
\WC()->cart->empty_cart( true );
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
private function graphql( $query, $operation_name = null, $variables = null ) {
|
||||
// Run GraphQL request.
|
||||
$results = graphql( compact( 'query', 'operation_name', 'variables' ) );
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $results );
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
private function addToCart( $input ) {
|
||||
$mutation = '
|
||||
@@ -53,15 +63,7 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
}
|
||||
';
|
||||
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'operation_name' => 'addToCart',
|
||||
'variables' => array( 'input' => $input ),
|
||||
)
|
||||
);
|
||||
|
||||
return $actual;
|
||||
return $this->graphql( $mutation, 'addToCart', compact( 'input' ) );
|
||||
}
|
||||
|
||||
private function removeItemsFromCart( $input ) {
|
||||
@@ -92,15 +94,7 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
}
|
||||
';
|
||||
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'operation_name' => 'removeItemsFromCart',
|
||||
'variables' => array( 'input' => $input ),
|
||||
)
|
||||
);
|
||||
|
||||
return $actual;
|
||||
return $this->graphql( $mutation, 'removeItemsFromCart', compact( 'input' ) );
|
||||
}
|
||||
|
||||
private function restoreItems( $input ) {
|
||||
@@ -129,17 +123,9 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
';
|
||||
';
|
||||
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'operation_name' => 'restoreCartItems',
|
||||
'variables' => array( 'input' => $input ),
|
||||
)
|
||||
);
|
||||
|
||||
return $actual;
|
||||
return $this->graphql( $mutation, 'restoreCartItems', compact( 'input' ) );
|
||||
}
|
||||
|
||||
// tests
|
||||
@@ -153,9 +139,6 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
// Retrieve cart item key.
|
||||
$this->assertArrayHasKey('data', $actual );
|
||||
$this->assertArrayHasKey('addToCart', $actual['data'] );
|
||||
@@ -202,9 +185,6 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
// Retrieve cart item key.
|
||||
$this->assertArrayHasKey('data', $actual );
|
||||
$this->assertArrayHasKey('addToCart', $actual['data'] );
|
||||
@@ -297,26 +277,21 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
}
|
||||
';
|
||||
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'operation_name' => 'updateItemQuantities',
|
||||
'variables' => array(
|
||||
'input' => array(
|
||||
'clientMutationId' => 'someId',
|
||||
'items' => array(
|
||||
array( 'key' => $key_1, 'quantity' => 4 ),
|
||||
array( 'key' => $key_2, 'quantity' => 2 ),
|
||||
array( 'key' => $key_3, 'quantity' => 0 ),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
$actual = $this->graphql(
|
||||
$mutation,
|
||||
'updateItemQuantities',
|
||||
array(
|
||||
'input' => array(
|
||||
'clientMutationId' => 'someId',
|
||||
'items' => array(
|
||||
array( 'key' => $key_1, 'quantity' => 4 ),
|
||||
array( 'key' => $key_2, 'quantity' => 2 ),
|
||||
array( 'key' => $key_3, 'quantity' => 0 ),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
// Check cart item data.
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
@@ -365,9 +340,6 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
'removeItemsFromCart' => array(
|
||||
@@ -425,9 +397,6 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
'removeItemsFromCart' => array(
|
||||
@@ -486,9 +455,6 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
'removeItemsFromCart' => array(
|
||||
@@ -537,9 +503,6 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
'restoreCartItems' => array(
|
||||
@@ -604,9 +567,6 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
'restoreCartItems' => array(
|
||||
@@ -666,16 +626,7 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
$variables = array(
|
||||
'input' => array( 'clientMutationId' => 'someId' ),
|
||||
);
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'operation_name' => 'emptyCart',
|
||||
'variables' => $variables,
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
$actual = $this->graphql( $mutation, 'emptyCart', $variables );
|
||||
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
@@ -771,16 +722,7 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
'code' => $coupon_code,
|
||||
),
|
||||
);
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'operation_name' => 'applyCoupon',
|
||||
'variables' => $variables,
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
$actual = $this->graphql( $mutation, 'applyCoupon', $variables );
|
||||
|
||||
// Get updated cart item.
|
||||
$cart_item = WC()->cart->get_cart_item( $cart_item_key );
|
||||
@@ -877,15 +819,7 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
'code' => $coupon_id,
|
||||
),
|
||||
);
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'variables' => $variables,
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
$actual = $this->graphql( $mutation, null, $variables );
|
||||
|
||||
$this->assertNotEmpty( $actual['errors'] );
|
||||
$this->assertEmpty( $actual['data']['applyCoupon'] );
|
||||
@@ -901,15 +835,7 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
'code' => $expired_coupon_code,
|
||||
),
|
||||
);
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'variables' => $variables,
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
$actual = $this->graphql( $mutation, null, $variables );
|
||||
|
||||
$this->assertNotEmpty( $actual['errors'] );
|
||||
$this->assertEmpty( $actual['data']['applyCoupon'] );
|
||||
@@ -925,15 +851,7 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
'code' => $applied_coupon_code,
|
||||
),
|
||||
);
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'variables' => $variables,
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
$actual = $this->graphql( $mutation, null, $variables );
|
||||
|
||||
$this->assertNotEmpty( $actual['errors'] );
|
||||
$this->assertEmpty( $actual['data']['applyCoupon'] );
|
||||
@@ -995,16 +913,7 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
'codes' => array( $coupon_code ),
|
||||
),
|
||||
);
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'operation_name' => 'removeCoupons',
|
||||
'variables' => $variables,
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
$actual = $this->graphql( $mutation, 'removeCoupons', $variables );
|
||||
|
||||
// Get updated cart item.
|
||||
$cart_item = \WC()->cart->get_cart_item( $cart_item_key );
|
||||
@@ -1078,30 +987,12 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
'amount' => 49.99,
|
||||
),
|
||||
);
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'operation_name' => 'addFee',
|
||||
'variables' => $variables,
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
$actual = $this->graphql( $mutation, 'addFee', $variables );
|
||||
|
||||
$this->assertArrayHasKey('errors', $actual );
|
||||
|
||||
wp_set_current_user( $this->shop_manager );
|
||||
$actual = graphql(
|
||||
array(
|
||||
'query' => $mutation,
|
||||
'operation_name' => 'addFee',
|
||||
'variables' => $variables,
|
||||
)
|
||||
);
|
||||
|
||||
// use --debug flag to view.
|
||||
codecept_debug( $actual );
|
||||
$actual = $this->graphql( $mutation, 'addFee', $variables );
|
||||
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
@@ -1113,5 +1004,25 @@ class CartMutationsTest extends \Codeception\TestCase\WPTestCase {
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
}
|
||||
|
||||
public function testAddToCartMutationErrors() {
|
||||
// Create products.
|
||||
$product_id = $this->product->create_simple(
|
||||
array(
|
||||
'manage_stock' => true,
|
||||
'stock_quantity' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$failed = $this->addToCart(
|
||||
array(
|
||||
'clientMutationId' => 'someId',
|
||||
'productId' => $product_id,
|
||||
'quantity' => 5,
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertArrayHasKey( 'errors', $failed );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user