2019-05-06 11:52:25 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - emptyCart
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for empty cart of all contents including coupons and fees.
|
|
|
|
|
*
|
2019-10-25 19:13:36 -04:00
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
2019-05-06 11:52:25 -04:00
|
|
|
* @since 0.1.0
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-25 19:13:36 -04:00
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
2019-05-06 11:52:25 -04:00
|
|
|
|
2020-11-23 14:46:41 -05:00
|
|
|
use GraphQL\Error\UserError;
|
2019-05-06 11:52:25 -04:00
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
2019-11-24 23:54:50 -05:00
|
|
|
use WPGraphQL\WooCommerce\Data\Mutation\Cart_Mutation;
|
2019-05-06 11:52:25 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class - Cart_Empty
|
|
|
|
|
*/
|
|
|
|
|
class Cart_Empty {
|
|
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2019-05-06 11:52:25 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
|
|
|
|
'emptyCart',
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
|
|
|
|
'inputFields' => [
|
|
|
|
|
'clearPersistentCart' => [ 'type' => 'Boolean' ],
|
|
|
|
|
],
|
2019-05-06 11:52:25 -04:00
|
|
|
'outputFields' => self::get_output_fields(),
|
|
|
|
|
'mutateAndGetPayload' => self::mutate_and_get_payload(),
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2019-05-06 11:52:25 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation output field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_output_fields() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
2020-10-13 15:01:44 -04:00
|
|
|
'deletedCart' => Cart_Mutation::get_cart_field(),
|
2022-08-26 14:53:36 -04:00
|
|
|
'cart' => [
|
2020-10-13 15:01:44 -04:00
|
|
|
'type' => 'Cart',
|
2023-07-19 22:56:42 +03:00
|
|
|
'resolve' => static function () {
|
2020-11-23 14:46:41 -05:00
|
|
|
return \WC()->cart;
|
2020-10-13 15:01:44 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
];
|
2019-05-06 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation data modification closure.
|
|
|
|
|
*
|
|
|
|
|
* @return callable
|
|
|
|
|
*/
|
|
|
|
|
public static function mutate_and_get_payload() {
|
2023-07-20 00:11:25 +03:00
|
|
|
return static function ( $input, AppContext $context, ResolveInfo $info ) {
|
2019-11-20 23:16:23 -05:00
|
|
|
Cart_Mutation::check_session_token();
|
|
|
|
|
|
2020-02-13 22:04:11 -06:00
|
|
|
// Get/Clone WC_Cart instance.
|
2019-05-06 11:52:25 -04:00
|
|
|
$cloned_cart = clone \WC()->cart;
|
|
|
|
|
|
2020-11-23 14:46:41 -05:00
|
|
|
if ( $cloned_cart->is_empty() ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Cart is empty', 'graphql-for-ecommerce' ) );
|
2020-11-23 14:46:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-13 22:04:11 -06:00
|
|
|
/**
|
|
|
|
|
* Action fired before cart was cleared/emptied.
|
|
|
|
|
*
|
|
|
|
|
* @param object $cloned_cart Cloned cart.
|
|
|
|
|
* @param array $input Input info.
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param \WPGraphQL\AppContext $context Context passed.
|
|
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info Resolver info passed.
|
2020-02-13 22:04:11 -06:00
|
|
|
*/
|
2020-02-16 00:04:40 -05:00
|
|
|
do_action( 'graphql_woocommerce_before_empty_cart', $cloned_cart, $input, $context, $info );
|
2019-07-11 18:08:02 -04:00
|
|
|
|
2019-05-06 11:52:25 -04:00
|
|
|
// Empty cart.
|
2020-11-23 14:46:41 -05:00
|
|
|
$clear_persistent_cart = ! empty( $input['clearPersistentCart'] ) ? $input['clearPersistentCart'] : true;
|
|
|
|
|
\WC()->cart->empty_cart( $clear_persistent_cart );
|
2019-05-06 11:52:25 -04:00
|
|
|
|
2020-02-13 22:04:11 -06:00
|
|
|
/**
|
|
|
|
|
* Action fired after cart was cleared/emptied.
|
|
|
|
|
*
|
|
|
|
|
* @param object $cloned_cart Cloned cart.
|
|
|
|
|
* @param array $input Input info.
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param \WPGraphQL\AppContext $context Context passed.
|
|
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info Resolver info passed.
|
2020-02-13 22:04:11 -06:00
|
|
|
*/
|
2020-02-16 00:04:40 -05:00
|
|
|
do_action( 'graphql_woocommerce_after_empty_cart', $cloned_cart, $input, $context, $info );
|
2019-07-11 18:08:02 -04:00
|
|
|
|
2024-08-07 12:39:45 -04:00
|
|
|
do_action( 'woographql_update_session', true );
|
|
|
|
|
|
2022-08-26 14:53:36 -04:00
|
|
|
return [ 'cart' => $cloned_cart ];
|
2019-05-06 11:52:25 -04:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|