-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from kidunot89/feature/update-order-mutation
updateOrder mutation
- Loading branch information
Showing
10 changed files
with
509 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
/** | ||
* Mutation - updateOrder | ||
* | ||
* Registers mutation for updating an existing order. | ||
* | ||
* @package WPGraphQL\Extensions\WooCommerce\Mutation | ||
* @since 0.2.0 | ||
*/ | ||
|
||
namespace WPGraphQL\Extensions\WooCommerce\Mutation; | ||
|
||
use GraphQL\Error\UserError; | ||
use GraphQL\Type\Definition\ResolveInfo; | ||
use GraphQLRelay\Relay; | ||
use WPGraphQL\AppContext; | ||
use WPGraphQL\Extensions\WooCommerce\Data\Mutation\Order_Mutation; | ||
use WPGraphQL\Extensions\WooCommerce\Model\Order; | ||
|
||
/** | ||
* Class Order_Update | ||
*/ | ||
class Order_Update { | ||
/** | ||
* Registers mutation | ||
*/ | ||
public static function register_mutation() { | ||
register_graphql_mutation( | ||
'updateOrder', | ||
array( | ||
'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() { | ||
$input_fields = array_merge( | ||
Order_Create::get_input_fields(), | ||
array( | ||
'id' => array( | ||
'type' => 'ID', | ||
'description' => __( 'Order global ID', 'wp-graphql-woocommerce' ), | ||
), | ||
'order' => array( | ||
'type' => 'Int', | ||
'description' => __( 'Order WP ID', 'wp-graphql-woocommerce' ), | ||
), | ||
'customerId' => array( | ||
'type' => 'Int', | ||
'description' => __( 'Order customer ID', 'wp-graphql-woocommerce' ), | ||
), | ||
) | ||
); | ||
|
||
return $input_fields; | ||
} | ||
|
||
/** | ||
* Defines the mutation output field configuration | ||
* | ||
* @return array | ||
*/ | ||
public static function get_output_fields() { | ||
return array( | ||
'order' => array( | ||
'type' => 'Order', | ||
'resolve' => function( $payload ) { | ||
return new Order( $payload['id'] ); | ||
}, | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Defines the mutation data modification closure. | ||
* | ||
* @return callable | ||
*/ | ||
public static function mutate_and_get_payload() { | ||
return function( $input, AppContext $context, ResolveInfo $info ) { | ||
$post_type_object = get_post_type_object( 'shop_order' ); | ||
|
||
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) { | ||
throw new UserError( __( 'Sorry, you are not allowed to update this order.', 'wp-graphql-woocommerce' ) ); | ||
} | ||
|
||
// Retrieve order ID. | ||
$order_id = null; | ||
if ( ! empty( $input['id'] ) ) { | ||
$id_components = Relay::fromGlobalId( $input['id'] ); | ||
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) { | ||
throw new UserError( __( 'The "id" provided is invalid', 'wp-graphql-woocommerce' ) ); | ||
} | ||
$order_id = absint( $id_components['id'] ); | ||
} elseif ( ! empty( $input['orderId'] ) ) { | ||
$order_id = absint( $input['orderId'] ); | ||
} else { | ||
throw new UserError( __( 'No order ID provided.', 'wp-graphql-woocommerce' ) ); | ||
} | ||
|
||
Order_Mutation::add_order_meta( $order_id, $input, $context, $info ); | ||
Order_Mutation::add_items( $input, $order_id, $context, $info ); | ||
|
||
// Apply coupons. | ||
if ( ! empty( $input['coupons'] ) ) { | ||
Order_Mutation::apply_coupons( $order_id, $input['coupons'] ); | ||
} | ||
|
||
$order = \WC_Order_Factory::get_order( $order_id ); | ||
|
||
// Make sure gateways are loaded so hooks from gateways fire on save/create. | ||
WC()->payment_gateways(); | ||
|
||
// Validate customer ID. | ||
if ( ! empty( $input['customerId'] ) ) { | ||
if ( ! Order_Mutation::validate_customer( $input ) ) { | ||
throw new UserError( __( 'New customer ID is invalid.', 'wp-graphql-woocommerce' ) ); | ||
} | ||
} | ||
|
||
$order->set_created_via( 'graphql-api' ); | ||
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) ); | ||
$order->calculate_totals( true ); | ||
|
||
// Set status. | ||
if ( ! empty( $input['status'] ) ) { | ||
$order->set_status( $input['status'] ); | ||
} | ||
|
||
// Actions for after the order is saved. | ||
if ( true === $input['isPaid'] ) { | ||
$order->payment_complete( | ||
! empty( $input['transactionId'] ) ? | ||
$input['transactionId'] | ||
: '' | ||
); | ||
} | ||
|
||
return array( 'id' => $order->get_id() ); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.