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

wp-graphql / wp-graphql-woocommerce / 11714183904

07 Nov 2024 12:23AM UTC coverage: 83.665% (-0.8%) from 84.451%
11714183904

push

github

web-flow
devops: Name officially changed to "WPGraphQL for WooCommerce" (#900)

* devops: Name officially changed to "WPGraphQL for WooCommerce"

* devops: GA workflow environment updated

* devops: GA workflow environment updated

* devops: composer-git-hooks downgraded to "2.8.5"

* devops: Unstable session manager tests skipped

* chore: cleanup applied

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

2 of 8 new or added lines in 5 files covered. (25.0%)

268 existing lines in 20 files now uncovered.

12431 of 14858 relevant lines covered (83.67%)

71.79 hits per line

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

92.31
/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(
141✔
31
                        'deleteOrderItems',
141✔
32
                        [
141✔
33
                                'inputFields'         => self::get_input_fields(),
141✔
34
                                'outputFields'        => self::get_output_fields(),
141✔
35
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
141✔
36
                        ]
141✔
37
                );
141✔
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(
141✔
47
                        [
141✔
48
                                'id'      => [
141✔
49
                                        'type'        => 'ID',
141✔
50
                                        'description' => __( 'Database ID or global ID of the order', 'wp-graphql-woocommerce' ),
141✔
51
                                ],
141✔
52
                                'orderId' => [
141✔
53
                                        'type'              => 'Int',
141✔
54
                                        'description'       => __( 'Order WP ID', 'wp-graphql-woocommerce' ),
141✔
55
                                        'deprecationReason' => __( 'Use "id" field instead.', 'wp-graphql-woocommerce' ),
141✔
56
                                ],
141✔
57
                                'itemIds' => [
141✔
58
                                        'type'        => [ 'list_of' => 'Int' ],
141✔
59
                                        'description' => __( 'ID Order items being deleted', 'wp-graphql-woocommerce' ),
141✔
60
                                ],
141✔
61
                        ]
141✔
62
                );
141✔
63
        }
64

65
        /**
66
         * Defines the mutation output field configuration
67
         *
68
         * @return array
69
         */
70
        public static function get_output_fields() {
71
                return [
141✔
72
                        'order' => [
141✔
73
                                'type'    => 'Order',
141✔
74
                                'resolve' => static function ( $payload ) {
141✔
75
                                        return $payload['order'];
1✔
76
                                },
141✔
77
                        ],
141✔
78
                ];
141✔
79
        }
80

81
        /**
82
         * Defines the mutation data modification closure.
83
         *
84
         * @return callable
85
         */
86
        public static function mutate_and_get_payload() {
87
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
141✔
88
                        // Retrieve order ID.
89
                        $order_id = null;
1✔
90
                        if ( ! empty( $input['id'] ) ) {
1✔
91
                                $order_id = Utils::get_database_id_from_id( $input['id'] );
×
92
                        } elseif ( ! empty( $input['orderId'] ) ) {
1✔
93
                                $order_id = absint( $input['orderId'] );
1✔
94
                        } else {
UNCOV
95
                                throw new UserError( __( 'Order ID provided is missing or invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
×
96
                        }
97

98
                        if ( ! $order_id ) {
1✔
UNCOV
99
                                throw new UserError( __( 'Order ID provided is invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
×
100
                        }
101

102
                        // Check if authorized to delete items on this order.
103
                        if ( ! Order_Mutation::authorized( $input, $context, $info, 'delete-items', $order_id ) ) {
1✔
104
                                throw new UserError( __( 'User does not have the capabilities necessary to delete order items.', 'wp-graphql-woocommerce' ) );
1✔
105
                        }
106

107
                        // Confirm item IDs.
108
                        if ( empty( $input['itemIds'] ) ) {
1✔
UNCOV
109
                                throw new UserError( __( 'No item IDs provided.', 'wp-graphql-woocommerce' ) );
×
110
                        } elseif ( ! is_array( $input['itemIds'] ) ) {
1✔
UNCOV
111
                                throw new UserError( __( 'The "itemIds" provided is invalid', 'wp-graphql-woocommerce' ) );
×
112
                        }
113
                        $ids = $input['itemIds'];
1✔
114

115
                        // Get Order model instance for output.
116
                        /**
117
                         * Order model instance.
118
                         *
119
                         * @var \WC_Order $order
120
                         */
121
                        $order = new Order( $order_id );
1✔
122

123
                        // Cache items to prevent null value errors.
124
                        // @codingStandardsIgnoreStart
125
                        $order->get_downloadable_items();
1✔
126
                        $order->get_items();
1✔
127
                        $order->get_items( 'fee' );
1✔
128
                        $order->get_items( 'shipping' );
1✔
129
                        $order->get_items( 'tax' );
1✔
130
                        $order->get_items( 'coupon' );
1✔
131
                        // @codingStandardsIgnoreEnd.
132

133
                        /**
134
                         * Working order model instance.
135
                         *
136
                         * @var \WC_Order $working_order
137
                         */
138
                        $working_order = new Order( $order_id );
1✔
139

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

151
                        // Delete order.
152
                        $errors = '';
1✔
153
                        foreach ( $ids as $id ) {
1✔
154
                                $working_order->remove_item( $id );
1✔
155
                        }
156
                        $working_order->save();
1✔
157

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

169
                        return [ 'order' => $order ];
1✔
170
                };
141✔
171
        }
172
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc