Files
wp-graphql-woocommerce/includes/mutation/class-cart-empty.php
T
4ef46acd15 chore: setup PHPStan (#746)
* chore: setup PHPStan

* chore: General code cleanup for PHPStan

* chore: PHPStan to level 2

* chore: PHPStan to level 2

* fix: Syntax error fixed.

* devops: PHPStan level 2 reached.

* devops: PHPStan level 3 passed

* devops: PHPStan level 4 passed

* chore: fix bad typehints from jwt-auth [phpstan]

* chore: simplify conditional logic in WC_Terms::register_connections()

* chore: add stubs for WP type properties.

* fix: check for valid WC_Order_Item before caching order.

* chore: lint

* devops: PHPStan level 6 passed.

* chore: Linter compliance met.

* chore: simplify logic and check for valid term.

* fix: ensure valid types (partial)

* chore: [phpcs] allow inline type annotations

* devops: PHPStan level 8 reached.

* devops: More stability fixes.

* fix: general bugfixes and improvements

* devops: Code Quality GA script added.

* devops: Linter compliance met & 2 "phpstan-ignore" statements set.

* devops: PHPStan config and CI script fixed.

* fix: "cartItem" query resolution refactored

* devops: GA scripts updated.

* chore: Simple copy changes

---------

Co-authored-by: Geoff Taylor <geoff@axistaylor.com>
2023-06-13 16:17:02 -04:00

102 lines
2.5 KiB
PHP

<?php
/**
* Mutation - emptyCart
*
* Registers mutation for empty cart of all contents including coupons and fees.
*
* @package WPGraphQL\WooCommerce\Mutation
* @since 0.1.0
*/
namespace WPGraphQL\WooCommerce\Mutation;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Data\Mutation\Cart_Mutation;
/**
* Class - Cart_Empty
*/
class Cart_Empty {
/**
* Registers mutation
*
* @return void
*/
public static function register_mutation() {
register_graphql_mutation(
'emptyCart',
[
'inputFields' => [
'clearPersistentCart' => [ 'type' => 'Boolean' ],
],
'outputFields' => self::get_output_fields(),
'mutateAndGetPayload' => self::mutate_and_get_payload(),
]
);
}
/**
* Defines the mutation output field configuration
*
* @return array
*/
public static function get_output_fields() {
return [
'deletedCart' => Cart_Mutation::get_cart_field(),
'cart' => [
'type' => 'Cart',
'resolve' => function () {
return \WC()->cart;
},
],
];
}
/**
* Defines the mutation data modification closure.
*
* @return callable
*/
public static function mutate_and_get_payload() {
return function( $input, AppContext $context, ResolveInfo $info ) {
Cart_Mutation::check_session_token();
// Get/Clone WC_Cart instance.
$cloned_cart = clone \WC()->cart;
if ( $cloned_cart->is_empty() ) {
throw new UserError( __( 'Cart is empty', 'wp-graphql-woocommerce' ) );
}
/**
* Action fired before cart was cleared/emptied.
*
* @param object $cloned_cart Cloned cart.
* @param array $input Input info.
* @param AppContext $context Context passed.
* @param ResolveInfo $info Resolver info passed.
*/
do_action( 'graphql_woocommerce_before_empty_cart', $cloned_cart, $input, $context, $info );
// Empty cart.
$clear_persistent_cart = ! empty( $input['clearPersistentCart'] ) ? $input['clearPersistentCart'] : true;
\WC()->cart->empty_cart( $clear_persistent_cart );
/**
* Action fired after cart was cleared/emptied.
*
* @param object $cloned_cart Cloned cart.
* @param array $input Input info.
* @param AppContext $context Context passed.
* @param ResolveInfo $info Resolver info passed.
*/
do_action( 'graphql_woocommerce_after_empty_cart', $cloned_cart, $input, $context, $info );
return [ 'cart' => $cloned_cart ];
};
}
}