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.
- Loading branch information
Showing
23 changed files
with
1,703 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,55 @@ | ||
<?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 ); | ||
} | ||
|
||
/** | ||
* Return array of data to be when defining a cart fee. | ||
* | ||
* @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_fee( $input, $context, $info ) { | ||
$cart_item_args = array( | ||
$input['name'], | ||
$input['amount'], | ||
! empty( $input['taxable'] ) ? $input['taxable'] : false, | ||
! empty( $input['taxClass'] ) ? $input['taxClass'] : '', | ||
); | ||
|
||
return apply_filters( 'woocommerce_new_cart_fee_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,106 @@ | ||
<?php | ||
/** | ||
* Mutation - addFee | ||
* | ||
* Registers mutation for add an additional fee 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_Fee | ||
*/ | ||
class Cart_Add_Fee { | ||
/** | ||
* Registers mutation | ||
*/ | ||
public static function register_mutation() { | ||
register_graphql_mutation( | ||
'addFee', | ||
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( | ||
'name' => array( | ||
'type' => array( 'non_null' => 'String' ), | ||
'description' => __( 'Unique name for the fee.', 'wp-graphql-woocommerce' ), | ||
), | ||
'amount' => array( | ||
'type' => 'Float', | ||
'description' => __( 'Fee amount', 'wp-graphql-woocommerce' ), | ||
), | ||
'taxable' => array( | ||
'type' => 'Boolean', | ||
'description' => __( 'Is the fee taxable?', 'wp-graphql-woocommerce' ), | ||
), | ||
'taxClass' => array( | ||
'type' => 'TaxClassEnum', | ||
'description' => __( 'The tax class for the fee if taxable.', 'wp-graphql-woocommerce' ), | ||
), | ||
); | ||
|
||
return $input_fields; | ||
} | ||
|
||
/** | ||
* Defines the mutation output field configuration | ||
* | ||
* @return array | ||
*/ | ||
public static function get_output_fields() { | ||
return array( | ||
'cartFee' => array( | ||
'type' => 'CartFee', | ||
'resolve' => function ( $payload ) { | ||
$fees = \WC()->cart->get_fees(); | ||
return $fees[ $payload['id'] ]; | ||
}, | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* 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['name'] ) ) { | ||
throw new UserError( __( 'No name provided for fee', 'wp-graphql-woocommerce' ) ); | ||
} | ||
if ( ! isset( $input['amount'] ) ) { | ||
throw new UserError( __( 'No amount set for the fee', 'wp-graphql-woocommerce' ) ); | ||
} | ||
|
||
// Get cart fee args. | ||
$cart_fee_args = Cart_Mutation::prepare_cart_fee( $input, $context, $info ); | ||
|
||
// Add cart fee. | ||
\WC()->cart->add_fee( ...$cart_fee_args ); | ||
|
||
// Return payload. | ||
return array( 'id' => \sanitize_title( $input['name'] ) ); | ||
}; | ||
} | ||
} |
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 ); | ||
}; | ||
} | ||
} |
Oops, something went wrong.