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
8 changed files
with
203 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
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
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