2019-05-10 23:47:28 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - removeItemsFromCart
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for removing cart item(s) from the cart.
|
|
|
|
|
*
|
2019-10-25 19:13:36 -04:00
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
2019-05-10 23:47:28 -04:00
|
|
|
* @since 0.1.0
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-25 19:13:36 -04:00
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
2019-05-10 23:47:28 -04:00
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
2019-10-25 19:13:36 -04:00
|
|
|
use WPGraphQL\WooCommerce\Data\Mutation\Cart_Mutation;
|
2019-05-10 23:47:28 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class - Cart_Remove_Items
|
|
|
|
|
*/
|
|
|
|
|
class Cart_Remove_Items {
|
|
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2019-05-10 23:47:28 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
|
|
|
|
'removeItemsFromCart',
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2019-05-10 23:47:28 -04:00
|
|
|
'inputFields' => self::get_input_fields(),
|
|
|
|
|
'outputFields' => self::get_output_fields(),
|
|
|
|
|
'mutateAndGetPayload' => self::mutate_and_get_payload(),
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2019-05-10 23:47:28 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation input field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_input_fields() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
|
|
|
|
'keys' => [
|
|
|
|
|
'type' => [ 'list_of' => 'ID' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Item keys of the items being removed', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'all' => [
|
2019-05-10 23:47:28 -04:00
|
|
|
'type' => 'Boolean',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Remove all cart items', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
];
|
2019-05-10 23:47:28 -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 [
|
|
|
|
|
'cartItems' => [
|
|
|
|
|
'type' => [ 'list_of' => 'CartItem' ],
|
2023-07-19 22:56:42 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2019-05-10 23:47:28 -04:00
|
|
|
return $payload['items'];
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
2019-12-16 17:42:03 -05:00
|
|
|
'cart' => Cart_Mutation::get_cart_field( true ),
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-05-10 23:47:28 -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();
|
|
|
|
|
|
2019-05-10 23:47:28 -04:00
|
|
|
if ( \WC()->cart->is_empty() ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'No items in cart to remove.', 'graphql-for-ecommerce' ) );
|
2019-05-10 23:47:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( empty( $input['keys'] ) && empty( $input['all'] ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'No cart item keys provided', 'graphql-for-ecommerce' ) );
|
2019-05-10 23:47:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cart_items = Cart_Mutation::retrieve_cart_items( $input, $context, $info, 'remove' );
|
|
|
|
|
foreach ( $cart_items as $item ) {
|
|
|
|
|
$success = \WC()->cart->remove_cart_item( $item['key'] );
|
|
|
|
|
if ( false === $success ) {
|
|
|
|
|
/* translators: Cart item removal failure message */
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( sprintf( __( 'Failed to remove item %s from cart.', 'graphql-for-ecommerce' ), $item['key'] ) );
|
2019-05-10 23:47:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 12:39:45 -04:00
|
|
|
do_action( 'woographql_update_session', true );
|
|
|
|
|
|
2019-05-10 23:47:28 -04:00
|
|
|
// Return payload.
|
2022-08-26 14:53:36 -04:00
|
|
|
return [ 'items' => $cart_items ];
|
2019-05-10 23:47:28 -04:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|