forked from wp-graphql/wp-graphql-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wp-graphql#56 from kidunot89/feature/add-item-to-c…
…art-mutation addToCart mutation
- Loading branch information
Showing
11 changed files
with
356 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Defines helper functions for executing mutations related to the cart. | ||
* | ||
* @package WPGraphQL\Extensions\WooCommerce\Data\Mutation | ||
* @since 0.1.0 | ||
*/ | ||
|
||
namespace WPGraphQL\Extensions\WooCommerce\Data\Mutation; | ||
|
||
/** | ||
* Class - Cart_Mutation | ||
*/ | ||
class Cart_Mutation { | ||
/** | ||
* Return array of data to be when defining a cart item | ||
* | ||
* @param array $input input data describing cart item. | ||
* @param AppContext $context AppContext instance. | ||
* @param ResolveInfo $info query info. | ||
* | ||
* @return array | ||
*/ | ||
public static function prepare_cart_item( $input, $context, $info ) { | ||
$cart_item_args = array( | ||
$input['productId'], | ||
! empty( $input['quantity'] ) ? $input['quantity'] : 1, | ||
! empty( $input['variationId'] ) ? $input['variationId'] : 0, | ||
! empty( $input['variation'] ) ? $input['variation'] : array(), | ||
! empty( $input['extraData'] ) ? json_decode( $input['extraData'], true ) : array(), | ||
); | ||
|
||
return apply_filters( 'woocommerce_new_cart_item_data', $cart_item_args, $input, $context, $info ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Mutation - addToCart | ||
* | ||
* Registers mutation for adding an cart item to the cart. | ||
* | ||
* @package WPGraphQL\Extensions\WooCommerce\Mutation | ||
* @since 0.1.0 | ||
*/ | ||
|
||
namespace WPGraphQL\Extensions\WooCommerce\Mutation; | ||
|
||
use GraphQL\Error\UserError; | ||
use GraphQL\Type\Definition\ResolveInfo; | ||
use WPGraphQL\AppContext; | ||
use WPGraphQL\Extensions\WooCommerce\Data\Mutation\Cart_Mutation; | ||
|
||
/** | ||
* Class - Cart_Add_Item | ||
*/ | ||
class Cart_Add_Item { | ||
/** | ||
* Registers mutation | ||
*/ | ||
public static function register_mutation() { | ||
register_graphql_mutation( | ||
'addToCart', | ||
array( | ||
'inputFields' => 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() { | ||
$input_fields = array( | ||
'productId' => array( | ||
'type' => array( 'non_null' => 'Int' ), | ||
'description' => __( 'Cart item product database ID or global ID', 'wp-graphql-woocommerce' ), | ||
), | ||
'quantity' => array( | ||
'type' => 'Int', | ||
'description' => __( 'Cart item quantity', 'wp-graphql-woocommerce' ), | ||
), | ||
'variationId' => array( | ||
'type' => 'Int', | ||
'description' => __( 'Cart item product variation database ID or global ID', 'wp-graphql-woocommerce' ), | ||
), | ||
'variation' => array( | ||
'type' => array( 'list_of' => 'ProductAttributeInput' ), | ||
'description' => __( 'Cart item product variation attributes', 'wp-graphql-woocommerce' ), | ||
), | ||
'extraData' => array( | ||
'type' => 'String', | ||
'description' => __( 'JSON string representation of extra cart item data', 'wp-graphql-woocommerce' ), | ||
), | ||
); | ||
|
||
return $input_fields; | ||
} | ||
|
||
/** | ||
* Defines the mutation output field configuration | ||
* | ||
* @return array | ||
*/ | ||
public static function get_output_fields() { | ||
return array( | ||
'cartItem' => array( | ||
'type' => 'CartItem', | ||
'resolve' => function ( $payload ) { | ||
$cart = WC()->cart; | ||
$item = $cart->get_cart_item( $payload['key'] ); | ||
|
||
return $item; | ||
}, | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Defines the mutation data modification closure. | ||
* | ||
* @return callable | ||
*/ | ||
public static function mutate_and_get_payload() { | ||
return function( $input, AppContext $context, ResolveInfo $info ) { | ||
// Retrieve product database ID if relay ID provided. | ||
if ( empty( $input['productId'] ) ) { | ||
throw new UserError( __( 'No product ID provided', 'wp-graphql-woocommerce' ) ); | ||
} | ||
|
||
// Prepare args for "add_to_cart" from input data. | ||
$cart_item_args = Cart_Mutation::prepare_cart_item( $input, $context, $info ); | ||
|
||
// Get WC_Cart instance. | ||
$cart = WC()->cart; | ||
|
||
// Add item to cart and get item key. | ||
$cart_item_key = $cart->add_to_cart( ...$cart_item_args ); | ||
|
||
// Return payload. | ||
return array( 'key' => $cart_item_key ); | ||
}; | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* WPInputObjectType - ProductAttributeInput | ||
* | ||
* @package \WPGraphQL\Extensions\WooCommerce\Type\WPInputObject | ||
* @since 0.1.0 | ||
*/ | ||
|
||
namespace WPGraphQL\Extensions\WooCommerce\Type\WPInputObject; | ||
|
||
/** | ||
* Class Product_Attribute_Input | ||
*/ | ||
class Product_Attribute_Input { | ||
/** | ||
* Registers type | ||
*/ | ||
public static function register() { | ||
register_graphql_input_type( | ||
'ProductAttributeInput', | ||
array( | ||
'description' => __( 'Options for ordering the connection', 'wp-graphql-woocommerce' ), | ||
'fields' => array( | ||
'attribute' => array( | ||
'type' => array( 'non_null' => 'String' ), | ||
), | ||
'attributeTerm' => array( | ||
'type' => array( 'non_null' => 'String' ), | ||
), | ||
), | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ public function create_simple( $args = array() ) { | |
$args | ||
) | ||
); | ||
|
||
return $product->save(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
class CartMutationsTest extends \Codeception\TestCase\WPTestCase { | ||
private $customer; | ||
private $product; | ||
private $variation; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
|
||
$this->customer = $this->getModule('\Helper\Wpunit')->customer(); | ||
$this->product = $this->getModule('\Helper\Wpunit')->product(); | ||
$this->variation = $this->getModule('\Helper\Wpunit')->product_variation(); | ||
} | ||
|
||
public function tearDown() { | ||
// your tear down methods here | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
private function addToCart( $input ) { | ||
$mutation = ' | ||
mutation addToCart( $input: AddToCartInput! ) { | ||
addToCart( input: $input ) { | ||
clientMutationId | ||
cartItem { | ||
key | ||
product { | ||
id | ||
} | ||
variation { | ||
id | ||
} | ||
quantity | ||
subtotal | ||
subtotalTax | ||
total | ||
tax | ||
} | ||
} | ||
} | ||
'; | ||
|
||
$variables = array( 'input' => $input ); | ||
$actual = graphql( | ||
array( | ||
'query' => $mutation, | ||
'operation_name' => 'addToCart', | ||
'variables' => $variables, | ||
) | ||
); | ||
|
||
return $actual; | ||
} | ||
|
||
// tests | ||
public function testAddToCartMutationWithProduct() { | ||
$product_id = $this->product->create_simple(); | ||
$actual = $this->addToCart( | ||
array( | ||
'clientMutationId' => 'someId', | ||
'productId' => $product_id, | ||
'quantity' => 2, | ||
) | ||
); | ||
|
||
// use --debug flag to view. | ||
codecept_debug( $actual ); | ||
|
||
// Retrieve cart item key. | ||
$this->assertArrayHasKey('data', $actual ); | ||
$this->assertArrayHasKey('addToCart', $actual['data'] ); | ||
$this->assertArrayHasKey('cartItem', $actual['data']['addToCart'] ); | ||
$this->assertArrayHasKey('key', $actual['data']['addToCart']['cartItem'] ); | ||
$key = $actual['data']['addToCart']['cartItem']['key']; | ||
|
||
// Get newly created cart item data. | ||
$cart = WC()->cart; | ||
$cart_item = $cart->get_cart_item( $key ); | ||
$this->assertNotEmpty( $cart_item ); | ||
|
||
// Check cart item data. | ||
$expected = array( | ||
'data' => array( | ||
'addToCart' => array( | ||
'clientMutationId' => 'someId', | ||
'cartItem' => array( | ||
'key' => $cart_item['key'], | ||
'product' => array( | ||
'id' => $this->product->to_relay_id( $cart_item['product_id'] ), | ||
), | ||
'variation' => null, | ||
'quantity' => $cart_item['quantity'], | ||
'subtotal' => floatval( $cart_item['line_subtotal'] ), | ||
'subtotalTax' => floatval( $cart_item['line_subtotal_tax'] ), | ||
'total' => floatval( $cart_item['line_total'] ), | ||
'tax' => floatval( $cart_item['line_tax'] ), | ||
), | ||
), | ||
), | ||
); | ||
$this->assertEqualSets( $expected, $actual ); | ||
} | ||
|
||
public function testAddToCartMutationWithProductVariation() { | ||
$ids = $this->variation->create( $this->product->create_variable() ); | ||
$actual = $this->addToCart( | ||
array( | ||
'clientMutationId' => 'someId', | ||
'productId' => $ids['product'], | ||
'quantity' => 3, | ||
'variationId' => $ids['variations'][0], | ||
) | ||
); | ||
|
||
// use --debug flag to view. | ||
codecept_debug( $actual ); | ||
|
||
// Retrieve cart item key. | ||
$this->assertArrayHasKey('data', $actual ); | ||
$this->assertArrayHasKey('addToCart', $actual['data'] ); | ||
$this->assertArrayHasKey('cartItem', $actual['data']['addToCart'] ); | ||
$this->assertArrayHasKey('key', $actual['data']['addToCart']['cartItem'] ); | ||
$key = $actual['data']['addToCart']['cartItem']['key']; | ||
|
||
// Get newly created cart item data. | ||
$cart = WC()->cart; | ||
$cart_item = $cart->get_cart_item( $key ); | ||
$this->assertNotEmpty( $cart_item ); | ||
|
||
$expected = array( | ||
'data' => array( | ||
'addToCart' => array( | ||
'clientMutationId' => 'someId', | ||
'cartItem' => array( | ||
'key' => $cart_item['key'], | ||
'product' => array( | ||
'id' => $this->product->to_relay_id( $cart_item['product_id'] ), | ||
), | ||
'variation' => array( | ||
'id' => $this->variation->to_relay_id( $cart_item['variation_id'] ), | ||
), | ||
'quantity' => $cart_item['quantity'], | ||
'subtotal' => floatval( $cart_item['line_subtotal'] ), | ||
'subtotalTax' => floatval( $cart_item['line_subtotal_tax'] ), | ||
'total' => floatval( $cart_item['line_total'] ), | ||
'tax' => floatval( $cart_item['line_tax'] ), | ||
), | ||
), | ||
), | ||
); | ||
$this->assertEqualSets( $expected, $actual ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.