Skip to content

Commit b7f52e2

Browse files
committed
Checkout mutation output fields updated.
1 parent 2520858 commit b7f52e2

File tree

8 files changed

+74
-42
lines changed

8 files changed

+74
-42
lines changed

includes/data/mutation/class-checkout-mutation.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@ protected static function maybe_skip_fieldset( $fieldset_key, $data ) {
6363
*/
6464
public static function prepare_checkout_args( $input, $context, $info ) {
6565
$data = array(
66-
'terms' => (int) isset( $input['terms'] ),
67-
'createaccount' => (int) ! empty( $input['account'] ),
68-
'payment_method' => isset( $input['paymentMethod'] ) ? $input['paymentMethod'] : '',
69-
'shipping_method' => isset( $input['shippingMethod'] ) ? $input['shippingMethod'] : '',
70-
'ship_to_different_address' => ! empty( $input['shipToDifferentAddress'] ) && ! wc_ship_to_billing_address_only(),
71-
'woocommerce_checkout_update_totals' => isset( $input['updateTotals'] ),
66+
'terms' => (int) isset( $input['terms'] ),
67+
'createaccount' => (int) ! empty( $input['account'] ),
68+
'payment_method' => isset( $input['paymentMethod'] ) ? $input['paymentMethod'] : '',
69+
'shipping_method' => isset( $input['shippingMethod'] ) ? $input['shippingMethod'] : '',
70+
'ship_to_different_address' => ! empty( $input['shipToDifferentAddress'] ) && ! wc_ship_to_billing_address_only(),
7271
);
7372
$skipped = array();
7473

@@ -460,6 +459,11 @@ protected function process_order_without_payment( $order_id ) {
460459
$order = wc_get_order( $order_id );
461460
$order->payment_complete();
462461
wc_empty_cart();
462+
463+
return array(
464+
'result' => 'success',
465+
'redirect' => apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $order->get_checkout_order_received_url(), $order ),
466+
);
463467
}
464468

465469
/**
@@ -468,12 +472,11 @@ protected function process_order_without_payment( $order_id ) {
468472
* @param array $data Order data.
469473
* @param AppContext $context AppContext instance.
470474
* @param ResolveInfo $info ResolveInfo instance.
475+
* @param array $results Order status.
471476
*
472477
* @throws UserError When validation fails.
473478
*/
474-
public static function process_checkout( $data, $context, $info ) {
475-
WC()->session->set( 'refresh_totals', true );
476-
479+
public static function process_checkout( $data, $context, $info, &$results = null ) {
477480
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
478481
wc_set_time_limit( 0 );
479482

@@ -491,26 +494,24 @@ public static function process_checkout( $data, $context, $info ) {
491494
// Validate posted data and cart items before proceeding.
492495
self::validate_checkout( $data );
493496

494-
if ( empty( $data['woocommerce_checkout_update_totals'] ) ) {
495-
self::process_customer( $data );
496-
$order_id = WC()->checkout->create_order( $data );
497-
$order = wc_get_order( $order_id );
497+
self::process_customer( $data );
498+
$order_id = WC()->checkout->create_order( $data );
499+
$order = wc_get_order( $order_id );
498500

499-
if ( is_wp_error( $order_id ) ) {
500-
throw new UserError( $order_id->get_error_message() );
501-
}
501+
if ( is_wp_error( $order_id ) ) {
502+
throw new UserError( $order_id->get_error_message() );
503+
}
502504

503-
if ( ! $order ) {
504-
throw new UserError( __( 'Unable to create order.', 'wp-graphql-woocommerce' ) );
505-
}
505+
if ( ! $order ) {
506+
throw new UserError( __( 'Unable to create order.', 'wp-graphql-woocommerce' ) );
507+
}
506508

507-
do_action( 'woocommerce_checkout_order_processed', $order_id, $data, $order );
509+
do_action( 'woocommerce_checkout_order_processed', $order_id, $data, $order );
508510

509-
if ( WC()->cart->needs_payment() ) {
510-
self::process_order_payment( $order_id, $data['payment_method'] );
511-
} else {
512-
self::process_order_without_payment( $order_id );
513-
}
511+
if ( WC()->cart->needs_payment() ) {
512+
$results = self::process_order_payment( $order_id, $data['payment_method'] );
513+
} else {
514+
$results = self::process_order_without_payment( $order_id );
514515
}
515516

516517
return $order_id;

includes/mutation/class-checkout.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ public static function get_input_fields() {
5555
'type' => 'Boolean',
5656
'description' => __( 'Ship to a separate address', 'wp-graphql-woocommerce' ),
5757
),
58-
'updateTotals' => array(
59-
'type' => 'Boolean',
60-
'description' => __( 'Update order totals', 'wp-graphql-woocommerce' ),
61-
),
6258
'paymentMethodTitle' => array(
6359
'type' => 'String',
6460
'description' => __( 'Payment method title.', 'woocommerce' ),
@@ -97,6 +93,18 @@ public static function get_output_fields() {
9793
return is_user_logged_in() ? new Customer( get_current_user_id() ) : null;
9894
},
9995
),
96+
'result' => array(
97+
'type' => 'String',
98+
'resolve' => function( $payload ) {
99+
return $payload['result'];
100+
},
101+
),
102+
'redirect' => array(
103+
'type' => 'String',
104+
'resolve' => function( $payload ) {
105+
return $payload['redirect'];
106+
},
107+
),
100108
);
101109
}
102110

@@ -122,7 +130,7 @@ public static function mutate_and_get_payload() {
122130
*/
123131
do_action( 'woocommerce_graphql_before_checkout', $args, $input, $context, $info );
124132

125-
$order_id = Checkout_Mutation::process_checkout( $args, $context, $info );
133+
$order_id = Checkout_Mutation::process_checkout( $args, $context, $info, $results );
126134

127135
if ( is_wp_error( $order_id ) ) {
128136
throw new UserError( $order_id->get_error_message( 'checkout-error' ) );
@@ -138,7 +146,7 @@ public static function mutate_and_get_payload() {
138146
*/
139147
do_action( 'woocommerce_graphql_after_checkout', $order_id, $input, $context, $info );
140148

141-
return array( 'id' => $order_id );
149+
return array_merge( array( 'id' => $order_id ), $results );
142150
} catch ( \Exception $e ) {
143151
Order_Mutation::purge( $order );
144152
throw new UserError( $e->getMessage() );

tests/_support/Helper/Acceptance.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public function checkout( array $input, $session_header = null ) {
224224
customer {
225225
id
226226
}
227+
result
228+
redirect
227229
}
228230
}
229231
';

tests/acceptance/NewCustomerCheckingOutCept.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@
5555
$I->assertArrayHasKey('data', $success );
5656
$I->assertArrayHasKey('checkout', $success['data'] );
5757
$I->assertArrayHasKey('order', $success['data']['checkout'] );
58+
$I->assertArrayHasKey('customer', $success['data']['checkout'] );
59+
$I->assertArrayHasKey('result', $success['data']['checkout'] );
60+
$I->assertEquals( 'success', $success['data']['checkout']['result'] );
61+
$I->assertArrayHasKey('redirect', $success['data']['checkout'] );
5862
$I->assertArrayHasKey('id', $success['data']['checkout']['order'] );

tests/wpunit/CheckoutMutationTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ private function checkout( $input ) {
220220
customer {
221221
id
222222
}
223+
result
224+
redirect
223225
}
224226
}
225227
';
@@ -294,6 +296,9 @@ public function testCheckoutOrderMutation() {
294296
$this->assertArrayHasKey('id', $actual['data']['checkout']['order'] );
295297
$order = \WC_Order_Factory::get_order( $actual['data']['checkout']['order']['orderId'] );
296298

299+
// Get Available payment gateways.
300+
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
301+
297302
$expected = array(
298303
'data' => array(
299304
'checkout' => array(
@@ -414,7 +419,9 @@ function( $item ) {
414419
),
415420
'customer' => array(
416421
'id' => $this->customer->to_relay_id( $order->get_customer_id() )
417-
)
422+
),
423+
'result' => 'success',
424+
'redirect' => $available_gateways['bacs']->process_payment( $order->get_id() )['redirect'],
418425
),
419426
)
420427
);
@@ -474,6 +481,9 @@ public function testCheckoutOrderMutationWithNewAccount() {
474481
$this->assertArrayHasKey('id', $actual['data']['checkout']['order'] );
475482
$order = \WC_Order_Factory::get_order( $actual['data']['checkout']['order']['orderId'] );
476483

484+
// Get Available payment gateways.
485+
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
486+
477487
$expected = array(
478488
'data' => array(
479489
'checkout' => array(
@@ -594,7 +604,9 @@ function( $item ) {
594604
),
595605
'customer' => array(
596606
'id' => $this->customer->to_relay_id( $order->get_customer_id() )
597-
)
607+
),
608+
'result' => 'success',
609+
'redirect' => $available_gateways['bacs']->process_payment( $order->get_id() )['redirect'],
598610
),
599611
)
600612
);
@@ -660,6 +672,9 @@ public function testCheckoutOrderMutationWithNoAccount() {
660672
$this->assertArrayHasKey('id', $actual['data']['checkout']['order'] );
661673
$order = \WC_Order_Factory::get_order( $actual['data']['checkout']['order']['orderId'] );
662674

675+
// Get Available payment gateways.
676+
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
677+
663678
$expected = array(
664679
'data' => array(
665680
'checkout' => array(
@@ -779,6 +794,8 @@ function( $item ) {
779794
)
780795
),
781796
'customer' => null,
797+
'result' => 'success',
798+
'redirect' => $available_gateways['bacs']->process_payment( $order->get_id() )['redirect'],
782799
),
783800
)
784801
);

vendor/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
require_once __DIR__ . '/composer/autoload_real.php';
66

7-
return ComposerAutoloaderInit72e2c552614a55e0e85344a2a0086ef3::getLoader();
7+
return ComposerAutoloaderInit47658c4b7d8904b82de7ca7f79505649::getLoader();

vendor/composer/autoload_real.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// autoload_real.php @generated by Composer
44

5-
class ComposerAutoloaderInit72e2c552614a55e0e85344a2a0086ef3
5+
class ComposerAutoloaderInit47658c4b7d8904b82de7ca7f79505649
66
{
77
private static $loader;
88

@@ -19,15 +19,15 @@ public static function getLoader()
1919
return self::$loader;
2020
}
2121

22-
spl_autoload_register(array('ComposerAutoloaderInit72e2c552614a55e0e85344a2a0086ef3', 'loadClassLoader'), true, true);
22+
spl_autoload_register(array('ComposerAutoloaderInit47658c4b7d8904b82de7ca7f79505649', 'loadClassLoader'), true, true);
2323
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
24-
spl_autoload_unregister(array('ComposerAutoloaderInit72e2c552614a55e0e85344a2a0086ef3', 'loadClassLoader'));
24+
spl_autoload_unregister(array('ComposerAutoloaderInit47658c4b7d8904b82de7ca7f79505649', 'loadClassLoader'));
2525

2626
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
2727
if ($useStaticLoader) {
2828
require_once __DIR__ . '/autoload_static.php';
2929

30-
call_user_func(\Composer\Autoload\ComposerStaticInit72e2c552614a55e0e85344a2a0086ef3::getInitializer($loader));
30+
call_user_func(\Composer\Autoload\ComposerStaticInit47658c4b7d8904b82de7ca7f79505649::getInitializer($loader));
3131
} else {
3232
$map = require __DIR__ . '/autoload_namespaces.php';
3333
foreach ($map as $namespace => $path) {

vendor/composer/autoload_static.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Composer\Autoload;
66

7-
class ComposerStaticInit72e2c552614a55e0e85344a2a0086ef3
7+
class ComposerStaticInit47658c4b7d8904b82de7ca7f79505649
88
{
99
public static $prefixLengthsPsr4 = array (
1010
'W' =>
@@ -139,9 +139,9 @@ class ComposerStaticInit72e2c552614a55e0e85344a2a0086ef3
139139
public static function getInitializer(ClassLoader $loader)
140140
{
141141
return \Closure::bind(function () use ($loader) {
142-
$loader->prefixLengthsPsr4 = ComposerStaticInit72e2c552614a55e0e85344a2a0086ef3::$prefixLengthsPsr4;
143-
$loader->prefixDirsPsr4 = ComposerStaticInit72e2c552614a55e0e85344a2a0086ef3::$prefixDirsPsr4;
144-
$loader->classMap = ComposerStaticInit72e2c552614a55e0e85344a2a0086ef3::$classMap;
142+
$loader->prefixLengthsPsr4 = ComposerStaticInit47658c4b7d8904b82de7ca7f79505649::$prefixLengthsPsr4;
143+
$loader->prefixDirsPsr4 = ComposerStaticInit47658c4b7d8904b82de7ca7f79505649::$prefixDirsPsr4;
144+
$loader->classMap = ComposerStaticInit47658c4b7d8904b82de7ca7f79505649::$classMap;
145145

146146
}, null, ClassLoader::class);
147147
}

0 commit comments

Comments
 (0)