mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
* perf: Migrate type descriptions to lazy lambdas
Convert all eager description strings from:
'description' => __( '...', 'wp-graphql-woocommerce' ),
to deferred lambdas:
'description' => static function () { return __( '...', 'wp-graphql-woocommerce' ); },
This defers the __() translation call until the description is actually
needed (e.g. during schema introspection), avoiding unnecessary
translation overhead on every GraphQL request. Mirrors the pattern
used in WPGraphQL core.
1206 descriptions converted across 169 files.
* chore: Linter compliances met
103 lines
2.4 KiB
PHP
103 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* Mutation - applyCoupon
|
|
*
|
|
* Registers mutation for applying a coupon.
|
|
*
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
|
* @since 0.1.0
|
|
*/
|
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
|
|
|
use GraphQL\Error\UserError;
|
|
use WPGraphQL\WooCommerce\Data\Mutation\Cart_Mutation;
|
|
|
|
/**
|
|
* Class - Cart_Apply_Coupon
|
|
*/
|
|
class Cart_Apply_Coupon {
|
|
/**
|
|
* Registers mutation
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function register_mutation() {
|
|
register_graphql_mutation(
|
|
'applyCoupon',
|
|
[
|
|
'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() {
|
|
return [
|
|
'code' => [
|
|
'type' => [ 'non_null' => 'String' ],
|
|
'description' => static function () {
|
|
return __( 'Code of coupon being applied', 'wp-graphql-woocommerce' );
|
|
},
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Defines the mutation output field configuration
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function get_output_fields() {
|
|
return [
|
|
'applied' => [
|
|
'type' => 'AppliedCoupon',
|
|
'resolve' => static function ( $payload ) {
|
|
return $payload['code'];
|
|
},
|
|
],
|
|
'cart' => Cart_Mutation::get_cart_field( true ),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Defines the mutation data modification closure.
|
|
*
|
|
* @return callable
|
|
*/
|
|
public static function mutate_and_get_payload() {
|
|
return static function ( $input ) {
|
|
Cart_Mutation::check_session_token();
|
|
|
|
$reason = '';
|
|
// If validate and successful applied to cart, return payload.
|
|
if ( Cart_Mutation::validate_coupon( $input['code'], $reason ) && \WC()->cart->apply_coupon( $input['code'] ) ) {
|
|
do_action( 'woographql_update_session', true );
|
|
|
|
return [ 'code' => $input['code'] ];
|
|
}
|
|
|
|
// If any session error notices, capture them.
|
|
$notices = \WC()->session->get( 'wc_notices' );
|
|
if ( ! empty( $notices['error'] ) ) {
|
|
$reason = implode( ' ', array_column( $notices['error'], 'notice' ) );
|
|
\wc_clear_notices();
|
|
}
|
|
|
|
// Throw any capture errors.
|
|
if ( ! empty( $reason ) ) {
|
|
throw new UserError( $reason );
|
|
}
|
|
|
|
// Throw for unknown failure.
|
|
throw new UserError( __( 'Failed to apply coupon. Check for an individual-use coupon on cart.', 'wp-graphql-woocommerce' ) );
|
|
};
|
|
}
|
|
}
|