forked from wp-graphql/wp-graphql-woocommerce
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,510 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ branches: | |
only: | ||
- master | ||
- develop | ||
- release-v0.1.0 | ||
|
||
cache: | ||
apt: true | ||
|
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
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 ); | ||
} | ||
} |
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,112 @@ | ||
<?php | ||
/** | ||
* Mutation - addToCart | ||
* | ||
* Registers mutation for adding a 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 ); | ||
}; | ||
} | ||
} |
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,107 @@ | ||
<?php | ||
/** | ||
* Mutation - applyCoupon | ||
* | ||
* Registers mutation for applying a coupon. | ||
* | ||
* @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; | ||
|
||
/** | ||
* Class - Cart_Apply_Coupon | ||
*/ | ||
class Cart_Apply_Coupon { | ||
/** | ||
* Registers mutation | ||
*/ | ||
public static function register_mutation() { | ||
register_graphql_mutation( | ||
'applyCoupon', | ||
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( | ||
'code' => array( | ||
'type' => array( 'non_null' => 'String' ), | ||
'description' => __( 'Code of coupon being applied', 'wp-graphql-woocommerce' ), | ||
), | ||
); | ||
|
||
return $input_fields; | ||
} | ||
|
||
/** | ||
* Defines the mutation output field configuration | ||
* | ||
* @return array | ||
*/ | ||
public static function get_output_fields() { | ||
return array( | ||
'cart' => array( | ||
'type' => 'Cart', | ||
'resolve' => function ( $payload ) { | ||
return $payload['cart']; | ||
}, | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* 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['code'] ) ) { | ||
throw new UserError( __( 'No coupon code provided', 'wp-graphql-woocommerce' ) ); | ||
} | ||
|
||
// Get the coupon. | ||
$the_coupon = new \WC_Coupon( $input['code'] ); | ||
|
||
// Prevent adding coupons by post ID. | ||
if ( $the_coupon->get_code() !== $input['code'] ) { | ||
throw new UserError( __( 'No coupon found with the code provided', 'wp-graphql-woocommerce' ) ); | ||
} | ||
|
||
// Check it can be used with cart. | ||
if ( ! $the_coupon->is_valid() ) { | ||
throw new UserError( $the_coupon->get_error_message() ); | ||
} | ||
|
||
// Check if applied. | ||
if ( \WC()->cart->has_discount( $input['code'] ) ) { | ||
throw new UserError( __( 'This coupon has already been applied to the cart', 'wp-graphql-woocommerce' ) ); | ||
} | ||
|
||
// Get cart item for payload. | ||
$success = \WC()->cart->apply_coupon( $input['code'] ); | ||
if ( false === $success ) { | ||
throw new UserError( __( 'Failed to apply coupon. Check for an individual-use coupon on cart.', 'wp-graphql-woocommerce' ) ); | ||
} | ||
|
||
// Return payload. | ||
return array( 'cart' => \WC()->cart ); | ||
}; | ||
} | ||
} |
Oops, something went wrong.