Skip to content

Commit

Permalink
Merge pull request #98 from kidunot89/feature/update-order-mutation
Browse files Browse the repository at this point in the history
updateOrder mutation
  • Loading branch information
kidunot89 authored Jul 5, 2019
2 parents 4f84381 + 02303ec commit 2ea80df
Show file tree
Hide file tree
Showing 10 changed files with 509 additions and 19 deletions.
2 changes: 2 additions & 0 deletions includes/class-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Remove_Coupons;
use WPGraphQL\Extensions\WooCommerce\Mutation\Cart_Add_Fee;
use WPGraphQL\Extensions\WooCommerce\Mutation\Order_Create;
use WPGraphQL\Extensions\WooCommerce\Mutation\Order_Update;

/**
* Class Actions
Expand Down Expand Up @@ -163,5 +164,6 @@ public static function graphql_register_types() {
Cart_Remove_Coupons::register_mutation();
Cart_Add_Fee::register_mutation();
Order_Create::register_mutation();
Order_Update::register_mutation();
}
}
6 changes: 4 additions & 2 deletions includes/data/mutation/class-order-mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function add_items( $input, $order_id, $context, $info ) {
foreach ( $items as $item_data ) {
// Create Order item.
$item_id = ( ! empty( $item_data['id'] ) && \WC_Order_Factory::get_order_item( $item_data['id'] ) )
? $item['id']
? $item_data['id']
: \wc_add_order_item( $order_id, array( 'order_item_type' => $type ) );

// Continue if order item creation failed.
Expand Down Expand Up @@ -113,7 +113,9 @@ protected static function map_input_to_item( $item_id, $input, $item_keys, $cont

// Calculate to subtotal/total for line items.
if ( isset( $args['quantity'] ) ) {
$product = wc_get_product( self::get_product_id( $args ) );
$product = ( ! empty( $order_item['product_id'] ) )
? wc_get_product( $order_item['product_id'] )
: wc_get_product( self::get_product_id( $args ) );
$total = wc_get_price_excluding_tax( $product, array( 'qty' => $args['quantity'] ) );
$args['subtotal'] = ! empty( $args['subtotal'] ) ? $args['subtotal'] : $total;
$args['total'] = ! empty( $args['total'] ) ? $args['total'] : $total;
Expand Down
1 change: 1 addition & 0 deletions includes/mutation/class-order-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function register_mutation() {
)
);
}

/**
* Defines the mutation input field configuration
*
Expand Down
149 changes: 149 additions & 0 deletions includes/mutation/class-order-update.php
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() );
};
}
}
12 changes: 8 additions & 4 deletions includes/type/input/class-fee-line-input.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ public static function register() {
array(
'description' => __( 'Fee line data.', 'wp-graphql-woocommerce' ),
'fields' => array(
'id' => array(
'type' => 'ID',
'description' => __( 'Fee Line ID', 'wp-graphql-woocommerce' ),
),
'name' => array(
'type' => array( 'non_null' => 'String' ),
'type' => 'String',
'description' => __( 'Fee name.', 'wp-graphql-woocommerce' ),
),
'taxClass' => array(
'type' => array( 'non_null' => 'TaxClassEnum' ),
'type' => 'TaxClassEnum',
'description' => __( 'Tax class of fee.', 'wp-graphql-woocommerce' ),
),
'taxStatus' => array(
'type' => array( 'non_null' => 'TaxStatusEnum' ),
'type' => 'TaxStatusEnum',
'description' => __( 'Tax status of fee.', 'wp-graphql-woocommerce' ),
),
'total' => array(
'type' => array( 'non_null' => 'String' ),
'type' => 'String',
'description' => __( 'Line total (after discounts).', 'wp-graphql-woocommerce' ),
),
),
Expand Down
6 changes: 5 additions & 1 deletion includes/type/input/class-line-item-input.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ public static function register() {
array(
'description' => __( 'Meta data.', 'wp-graphql-woocommerce' ),
'fields' => array(
'id' => array(
'type' => 'ID',
'description' => __( 'Line Item ID', 'wp-graphql-woocommerce' ),
),
'name' => array(
'type' => 'String',
'description' => __( 'Line name', 'wp-graphql-woocommerce' ),
),
'productId' => array(
'type' => array( 'non_null' => 'Int' ),
'type' => 'Int',
'description' => __( 'Product ID.', 'wp-graphql-woocommerce' ),
),
'variationId' => array(
Expand Down
4 changes: 4 additions & 0 deletions includes/type/input/class-shipping-line-input.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static function register() {
array(
'description' => __( 'Shipping lines data.', 'wp-graphql-woocommerce' ),
'fields' => array(
'id' => array(
'type' => 'ID',
'description' => __( 'Shipping Line ID', 'wp-graphql-woocommerce' ),
),
'methodTitle' => array(
'type' => array( 'non_null' => 'String' ),
'description' => __( 'Shipping method name.', 'wp-graphql-woocommerce' ),
Expand Down
Loading

0 comments on commit 2ea80df

Please sign in to comment.