Files

179 lines
5.0 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Mutation - deleteOrder
*
* Registers mutation for delete an order.
*
2019-10-25 19:13:36 -04:00
* @package WPGraphQL\WooCommerce\Mutation
* @since 0.2.0
*/
2019-10-25 19:13:36 -04:00
namespace WPGraphQL\WooCommerce\Mutation;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WC_Order_Factory;
use WPGraphQL\AppContext;
use WPGraphQL\Utils\Utils;
2019-10-25 19:13:36 -04:00
use WPGraphQL\WooCommerce\Data\Mutation\Order_Mutation;
use WPGraphQL\WooCommerce\Model\Order;
/**
* Class Order_Delete
*/
class Order_Delete {
/**
* Registers mutation
2023-06-13 23:17:02 +03:00
*
* @return void
*/
public static function register_mutation() {
register_graphql_mutation(
'deleteOrder',
[
'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 array_merge(
[
'id' => [
'type' => 'ID',
'description' => static function () {
return __( 'Database ID or global ID of the order', 'graphql-for-ecommerce' );
},
],
'orderId' => [
'type' => 'Int',
'description' => static function () {
return __( 'Order WP ID', 'graphql-for-ecommerce' );
},
'deprecationReason' => __( 'Use "id" field instead.', 'graphql-for-ecommerce' ),
],
'forceDelete' => [
'type' => 'Boolean',
'description' => static function () {
return __( 'Delete or simply place in trash.', 'graphql-for-ecommerce' );
},
],
]
);
}
/**
* Defines the mutation output field configuration
*
* @return array
*/
public static function get_output_fields() {
return [
'order' => [
'type' => 'Order',
'resolve' => static function ( $payload ) {
return $payload['order'];
},
],
];
}
/**
* Defines the mutation data modification closure.
*
* @return callable
*/
public static function mutate_and_get_payload() {
return static function ( $input, AppContext $context, ResolveInfo $info ) {
// Retrieve order ID.
$order_id = false;
if ( ! empty( $input['id'] ) ) {
$order_id = Utils::get_database_id_from_id( $input['id'] );
} elseif ( ! empty( $input['orderId'] ) ) {
$order_id = absint( $input['orderId'] );
} else {
throw new UserError( __( 'Order ID provided is missing or invalid. Please check input and try again.', 'graphql-for-ecommerce' ) );
}
if ( ! $order_id ) {
throw new UserError( __( 'Order ID provided is invalid. Please check input and try again.', 'graphql-for-ecommerce' ) );
}
2020-01-13 23:36:23 -05:00
// Check if authorized to delete this order.
if ( ! Order_Mutation::authorized( $input, $context, $info, 'delete', $order_id ) ) {
throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'graphql-for-ecommerce' ) );
2020-01-13 23:36:23 -05:00
}
$force_delete = false;
if ( ! empty( $input['forceDelete'] ) ) {
$force_delete = $input['forceDelete'];
}
2023-06-13 23:17:02 +03:00
/**
* Get Order model instance for output.
*
* @var \WC_Order $order
*/
$order = new Order( $order_id );
// Cache items to prevent null value errors.
// @codingStandardsIgnoreStart
2023-06-13 23:17:02 +03:00
$order->get_downloadable_items();
$order->get_items();
$order->get_items( 'fee' );
$order->get_items( 'shipping' );
$order->get_items( 'tax' );
$order->get_items( 'coupon' );
// @codingStandardsIgnoreEnd.
/**
* Action called before order is deleted.
*
* @param \WC_Order|\WPGraphQL\WooCommerce\Model\Order $order Order model instance.
2023-06-13 23:17:02 +03:00
* @param array $input Input data describing order.
* @param \WPGraphQL\AppContext $context Request AppContext instance.
* @param \GraphQL\Type\Definition\ResolveInfo $info Request ResolveInfo instance.
*/
do_action( 'graphql_woocommerce_before_order_delete', $order, $input, $context, $info );
// Delete order.
2023-06-13 23:17:02 +03:00
$order_to_be_deleted = WC_Order_Factory::get_order( $order->get_id() );
if ( ! is_object( $order_to_be_deleted ) ) {
throw new UserError( __( 'Order to be deleted could not be found.', 'graphql-for-ecommerce' ) );
2023-06-13 23:17:02 +03:00
}
$success = Order_Mutation::purge( $order_to_be_deleted, $force_delete );
if ( ! $success ) {
throw new UserError(
sprintf(
/* translators: Deletion failed message */
__( 'Removal of Order %d failed', 'graphql-for-ecommerce' ),
$order->get_id()
)
);
}
2019-07-06 11:00:43 -04:00
/**
* Action called before order is deleted.
*
* @param \WC_Order|\WPGraphQL\WooCommerce\Model\Order $order Order model instance.
2023-06-13 23:17:02 +03:00
* @param array $input Input data describing order
* @param \WPGraphQL\AppContext $context Request AppContext instance.
* @param \GraphQL\Type\Definition\ResolveInfo $info Request ResolveInfo instance.
2019-07-06 11:00:43 -04:00
*/
do_action( 'graphql_woocommerce_after_order_delete', $order, $input, $context, $info );
2019-07-06 11:00:43 -04:00
return [ 'order' => $order ];
};
}
}