• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 27386231983

12 Jun 2026 12:25AM UTC coverage: 91.791%. Remained the same
27386231983

Pull #1019

github

web-flow
Merge 46a421a18 into 01876f534
Pull Request #1019: fix: address WordPress.org plugin review (rename + prefixing + headers)

1327 of 1584 new or added lines in 200 files covered. (83.78%)

1 existing line in 1 file now uncovered.

18505 of 20160 relevant lines covered (91.79%)

151.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.96
/includes/mutation/class-order-delete-items.php
1
<?php
2
/**
3
 * Mutation - deleteOrderItems
4
 *
5
 * Registers mutation for delete an items from an order.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since 0.2.0
9
 */
10

11
namespace WPGraphQL\WooCommerce\Mutation;
12

13
use GraphQL\Error\UserError;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use WPGraphQL\AppContext;
16
use WPGraphQL\Utils\Utils;
17
use WPGraphQL\WooCommerce\Data\Mutation\Order_Mutation;
18
use WPGraphQL\WooCommerce\Model\Order;
19

20
/**
21
 * Class Order_Delete_Items
22
 */
23
class Order_Delete_Items {
24
        /**
25
         * Registers mutation
26
         *
27
         * @return void
28
         */
29
        public static function register_mutation() {
30
                register_graphql_mutation(
296✔
31
                        'deleteOrderItems',
296✔
32
                        [
296✔
33
                                'inputFields'         => self::get_input_fields(),
296✔
34
                                'outputFields'        => self::get_output_fields(),
296✔
35
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
296✔
36
                        ]
296✔
37
                );
296✔
38
        }
39

40
        /**
41
         * Defines the mutation input field configuration
42
         *
43
         * @return array
44
         */
45
        public static function get_input_fields() {
46
                return array_merge(
296✔
47
                        [
296✔
48
                                'id'      => [
296✔
49
                                        'type'        => 'ID',
296✔
50
                                        'description' => static function () {
296✔
51
                                                return __( 'Database ID or global ID of the order', 'graphql-for-ecommerce' );
2✔
52
                                        },
296✔
53
                                ],
296✔
54
                                'orderId' => [
296✔
55
                                        'type'              => 'Int',
296✔
56
                                        'description'       => static function () {
296✔
57
                                                return __( 'Order WP ID', 'graphql-for-ecommerce' );
2✔
58
                                        },
296✔
59
                                        'deprecationReason' => __( 'Use "id" field instead.', 'graphql-for-ecommerce' ),
296✔
60
                                ],
296✔
61
                                'itemIds' => [
296✔
62
                                        'type'        => [ 'list_of' => 'Int' ],
296✔
63
                                        'description' => static function () {
296✔
64
                                                return __( 'ID Order items being deleted', 'graphql-for-ecommerce' );
2✔
65
                                        },
296✔
66
                                ],
296✔
67
                        ]
296✔
68
                );
296✔
69
        }
70

71
        /**
72
         * Defines the mutation output field configuration
73
         *
74
         * @return array
75
         */
76
        public static function get_output_fields() {
77
                return [
296✔
78
                        'order' => [
296✔
79
                                'type'    => 'Order',
296✔
80
                                'resolve' => static function ( $payload ) {
296✔
81
                                        return $payload['order'];
1✔
82
                                },
296✔
83
                        ],
296✔
84
                ];
296✔
85
        }
86

87
        /**
88
         * Defines the mutation data modification closure.
89
         *
90
         * @return callable
91
         */
92
        public static function mutate_and_get_payload() {
93
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
296✔
94
                        // Retrieve order ID.
95
                        $order_id = null;
1✔
96
                        if ( ! empty( $input['id'] ) ) {
1✔
97
                                $order_id = Utils::get_database_id_from_id( $input['id'] );
×
98
                        } elseif ( ! empty( $input['orderId'] ) ) {
1✔
99
                                $order_id = absint( $input['orderId'] );
1✔
100
                        } else {
NEW
101
                                throw new UserError( __( 'Order ID provided is missing or invalid. Please check input and try again.', 'graphql-for-ecommerce' ) );
×
102
                        }
103

104
                        if ( ! $order_id ) {
1✔
NEW
105
                                throw new UserError( __( 'Order ID provided is invalid. Please check input and try again.', 'graphql-for-ecommerce' ) );
×
106
                        }
107

108
                        // Check if authorized to delete items on this order.
109
                        if ( ! Order_Mutation::authorized( $input, $context, $info, 'delete-items', $order_id ) ) {
1✔
110
                                throw new UserError( __( 'User does not have the capabilities necessary to delete order items.', 'graphql-for-ecommerce' ) );
1✔
111
                        }
112

113
                        // Confirm item IDs.
114
                        if ( empty( $input['itemIds'] ) ) {
1✔
NEW
115
                                throw new UserError( __( 'No item IDs provided.', 'graphql-for-ecommerce' ) );
×
116
                        } elseif ( ! is_array( $input['itemIds'] ) ) {
1✔
NEW
117
                                throw new UserError( __( 'The "itemIds" provided is invalid', 'graphql-for-ecommerce' ) );
×
118
                        }
119
                        $ids = $input['itemIds'];
1✔
120

121
                        // Get Order model instance for output.
122
                        /**
123
                         * Order model instance.
124
                         *
125
                         * @var \WC_Order $order
126
                         */
127
                        $order = new Order( $order_id );
1✔
128

129
                        // Cache items to prevent null value errors.
130
                        // @codingStandardsIgnoreStart
131
                        $order->get_downloadable_items();
1✔
132
                        $order->get_items();
1✔
133
                        $order->get_items( 'fee' );
1✔
134
                        $order->get_items( 'shipping' );
1✔
135
                        $order->get_items( 'tax' );
1✔
136
                        $order->get_items( 'coupon' );
1✔
137
                        // @codingStandardsIgnoreEnd.
138

139
                        /**
140
                         * Working order model instance.
141
                         *
142
                         * @var \WC_Order $working_order
143
                         */
144
                        $working_order = new Order( $order_id );
1✔
145

146
                        /**
147
                         * Action called before order is deleted.
148
                         *
149
                         * @param array           $item_ids  Order item IDs of items being deleted.
150
                         * @param \WC_Order|\WPGraphQL\WooCommerce\Model\Order $order     Order model instance.
151
                         * @param array           $input     Input data describing order.
152
                         * @param \WPGraphQL\AppContext      $context   Request AppContext instance.
153
                         * @param \GraphQL\Type\Definition\ResolveInfo     $info      Request ResolveInfo instance.
154
                         */
155
                        do_action( 'graphql_woocommerce_before_order_items_delete', $ids, $working_order, $input, $context, $info );
1✔
156

157
                        // Delete order.
158
                        $errors = '';
1✔
159
                        foreach ( $ids as $id ) {
1✔
160
                                $working_order->remove_item( $id );
1✔
161
                        }
162
                        $working_order->save();
1✔
163

164
                        /**
165
                         * Action called before order is deleted.
166
                         *
167
                         * @param array           $item_ids  Order item IDs of items being deleted.
168
                         * @param \WC_Order|\WPGraphQL\WooCommerce\Model\Order $order     Order model instance.
169
                         * @param array           $input     Input data describing order
170
                         * @param \WPGraphQL\AppContext      $context   Request AppContext instance.
171
                         * @param \GraphQL\Type\Definition\ResolveInfo     $info      Request ResolveInfo instance.
172
                         */
173
                        do_action( 'graphql_woocommerce_after_order_delete', $ids, $working_order, $input, $context, $info );
1✔
174

175
                        return [ 'order' => $order ];
1✔
176
                };
296✔
177
        }
178
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc