2019-05-06 15:35:49 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2019-05-11 00:40:48 -04:00
|
|
|
* Mutation - removeCoupons
|
2019-05-06 15:35:49 -04:00
|
|
|
*
|
2019-05-11 00:40:48 -04:00
|
|
|
* Registers mutation for removing coupon(s) from cart.
|
2019-05-06 15:35:49 -04:00
|
|
|
*
|
2019-10-25 19:13:36 -04:00
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
2019-05-06 15:35:49 -04:00
|
|
|
* @since 0.1.0
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-25 19:13:36 -04:00
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
2019-05-06 15:35:49 -04:00
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
2019-11-24 23:54:50 -05:00
|
|
|
use WPGraphQL\WooCommerce\Data\Mutation\Cart_Mutation;
|
2019-05-06 15:35:49 -04:00
|
|
|
|
|
|
|
|
/**
|
2019-05-11 00:40:48 -04:00
|
|
|
* Class - Cart_Remove_Coupons
|
2019-05-06 15:35:49 -04:00
|
|
|
*/
|
2019-05-11 00:40:48 -04:00
|
|
|
class Cart_Remove_Coupons {
|
2019-05-06 15:35:49 -04:00
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2019-05-06 15:35:49 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
2019-05-11 00:40:48 -04:00
|
|
|
'removeCoupons',
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2019-05-06 15:35:49 -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-06 15:35:49 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-13 22:04:11 -06:00
|
|
|
* Defines the mutation input field configuration.
|
2019-05-06 15:35:49 -04:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_input_fields() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
|
|
|
|
'codes' => [
|
|
|
|
|
'type' => [ 'list_of' => 'String' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Code of coupon being applied', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
];
|
2019-05-06 15:35:49 -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 [
|
2019-12-16 17:42:03 -05:00
|
|
|
'cart' => Cart_Mutation::get_cart_field(),
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2019-05-06 15:35:49 -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 ) {
|
2019-11-20 23:16:23 -05:00
|
|
|
Cart_Mutation::check_session_token();
|
|
|
|
|
|
2019-05-06 15:35:49 -04:00
|
|
|
// Retrieve product database ID if relay ID provided.
|
2019-05-11 00:40:48 -04:00
|
|
|
if ( empty( $input['codes'] ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'No coupon codes provided', 'graphql-for-ecommerce' ) );
|
2019-05-06 15:35:49 -04:00
|
|
|
}
|
|
|
|
|
|
2019-05-11 00:40:48 -04:00
|
|
|
foreach ( $input['codes'] as $code ) {
|
2019-05-06 15:35:49 -04:00
|
|
|
|
2019-05-11 00:40:48 -04:00
|
|
|
// Check if applied.
|
|
|
|
|
if ( ! \WC()->cart->has_discount( $code ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'This coupon has not been applied to the cart.', 'graphql-for-ecommerce' ) );
|
2019-05-11 00:40:48 -04:00
|
|
|
}
|
2019-05-06 15:35:49 -04:00
|
|
|
|
2019-05-11 00:40:48 -04:00
|
|
|
// Get cart item for payload.
|
|
|
|
|
$success = \WC()->cart->remove_coupon( $code );
|
|
|
|
|
if ( true !== $success ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Failed to remove coupon.', 'graphql-for-ecommerce' ) );
|
2019-05-11 00:40:48 -04:00
|
|
|
}
|
2019-05-06 15:35:49 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-24 00:17:12 -04:00
|
|
|
// Recalculate totals after coupon removal.
|
|
|
|
|
\WC()->cart->calculate_totals();
|
|
|
|
|
|
2024-08-07 12:39:45 -04:00
|
|
|
do_action( 'woographql_update_session', true );
|
|
|
|
|
|
2019-05-06 15:35:49 -04:00
|
|
|
// Return payload.
|
2022-08-26 14:53:36 -04:00
|
|
|
return [ 'cart' => \WC()->cart ];
|
2019-05-06 15:35:49 -04:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|