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

wp-graphql / wp-graphql-woocommerce / 9122745284

17 May 2024 04:03AM UTC coverage: 85.048% (+0.4%) from 84.647%
9122745284

push

github

web-flow
feat: Queries and mutations for shipping zones, tax classes, and tax rates. (#856)

* fix: General bugfixes and improvements

* devops: New mutations and types tested and compliance with Linter and PHPStan

* chore: hooks added to the mutation resolvers

* feat: permission checks added

* chore: Linter and compliance met

* chore: Linter and compliance met

1252 of 1399 new or added lines in 39 files covered. (89.49%)

9 existing lines in 2 files now uncovered.

12423 of 14607 relevant lines covered (85.05%)

70.56 hits per line

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

92.5
/includes/mutation/class-shipping-zone-method-update.php
1
<?php
2
/**
3
 * Mutation - updateMethodOnShippingZone
4
 *
5
 * Registers mutation for update a shipping method on a shipping zone.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since TBD
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\WooCommerce\Data\Mutation\Shipping_Mutation;
17
use WPGraphQL\WooCommerce\Model\Shipping_Method;
18

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

39
        /**
40
         * Defines the mutation input field configuration
41
         *
42
         * @return array
43
         */
44
        public static function get_input_fields() {
45
                return [
138✔
46
                        'zoneId'     => [
138✔
47
                                'type'        => [ 'non_null' => 'Int' ],
138✔
48
                                'description' => __( 'The ID of the shipping zone to delete.', 'wp-graphql-woocommerce' ),
138✔
49
                        ],
138✔
50
                        'instanceId' => [
138✔
51
                                'type'        => [ 'non_null' => 'Int' ],
138✔
52
                                'description' => __( 'Shipping method instance ID', 'wp-graphql-woocommerce' ),
138✔
53
                        ],
138✔
54
                        'enabled'    => [
138✔
55
                                'type'        => 'Boolean',
138✔
56
                                'description' => __( 'Whether the shipping method is enabled or not.', 'wp-graphql-woocommerce' ),
138✔
57
                        ],
138✔
58
                        'order'      => [
138✔
59
                                'type'        => 'Int',
138✔
60
                                'description' => __( 'The order of the shipping method.', 'wp-graphql-woocommerce' ),
138✔
61
                        ],
138✔
62
                        'settings'   => [
138✔
63
                                'type'        => [ 'list_of' => 'WCSettingInput' ],
138✔
64
                                'description' => __( 'The settings for the shipping method.', 'wp-graphql-woocommerce' ),
138✔
65
                        ],
138✔
66
                ];
138✔
67
        }
68

69
        /**
70
         * Defines the mutation output field configuration
71
         *
72
         * @return array
73
         */
74
        public static function get_output_fields() {
75
                return [
138✔
76
                        'shippingZone' => [
138✔
77
                                'type'    => 'ShippingZone',
138✔
78
                                'resolve' => static function ( $payload, array $args, AppContext $context ) {
138✔
79
                                        return $context->get_loader( 'shipping_zone' )->load( $payload['zone_id'] );
1✔
80
                                },
138✔
81
                        ],
138✔
82
                        'method'       => [
138✔
83
                                'type'    => 'ShippingZoneToShippingMethodConnectionEdge',
138✔
84
                                'resolve' => static function ( $payload, array $args, AppContext $context ) {
138✔
85
                                        return [
1✔
86
                                                // Call the Shipping_Method constructor directly because "$payload['method']" is a non-scalar value.
87
                                                'node'   => new Shipping_Method( $payload['method'] ),
1✔
88
                                                'source' => $context->get_loader( 'shipping_zone' )->load( $payload['zone_id'] ),
1✔
89
                                        ];
1✔
90
                                },
138✔
91
                        ],
138✔
92
                ];
138✔
93
        }
94

95
        /**
96
         * Defines the mutation data modification closure.
97
         *
98
         * @return callable
99
         */
100
        public static function mutate_and_get_payload() {
101
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
138✔
102
                        if ( ! \wc_shipping_enabled() ) {
1✔
NEW
103
                                throw new UserError( __( 'Shipping is disabled.', 'wp-graphql-woocommerce' ), 404 );
×
104
                        }
105

106
                        if ( ! \wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
1✔
107
                                throw new UserError( __( 'Sorry, you are not allowed to edit shipping methods.', 'wp-graphql-woocommerce' ), \rest_authorization_required_code() );
1✔
108
                        }
109
                        $instance_id = $input['instanceId'];
1✔
110
                        $zone_id     = $input['zoneId'];
1✔
111
                        /** @var \WC_Shipping_Zone|false $zone */
112
                        $zone = \WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id );
1✔
113

114
                        if ( false === $zone ) {
1✔
NEW
115
                                throw new UserError( __( 'Invalid shipping zone ID.', 'wp-graphql-woocommerce' ) );
×
116
                        }
117

118
                        if ( 0 === $zone->get_id() ) {
1✔
NEW
119
                                throw new UserError( __( 'Invalid shipping zone ID.', 'wp-graphql-woocommerce' ) );
×
120
                        }
121

122
                        $methods = $zone->get_shipping_methods();
1✔
123
                        $method  = false;
1✔
124

125
                        foreach ( $methods as $shipping_method ) {
1✔
126
                                if ( $shipping_method->instance_id === $instance_id ) {
1✔
127
                                        $method = $shipping_method;
1✔
128
                                        break;
1✔
129
                                }
130
                        }
131

132
                        if ( ! $method ) {
1✔
NEW
133
                                throw new UserError( __( 'Invalid shipping method instance ID.', 'wp-graphql-woocommerce' ) );
×
134
                        }
135

136
                        // Update settings.
137
                        if ( ! empty( $input['settings'] ) ) {
1✔
138
                                $method = Shipping_Mutation::set_shipping_zone_method_settings( $instance_id, $method, $input['settings'] );
1✔
139
                        }
140

141
                        // Update order.
142
                        if ( isset( $input['order'] ) ) {
1✔
NEW
143
                                $method = Shipping_Mutation::set_shipping_zone_method_order( $instance_id, $method, $input['order'] );
×
144
                        }
145

146
                        // Update if this method is enabled or not.
147
                        if ( isset( $input['enabled'] ) ) {
1✔
NEW
148
                                $method = Shipping_Mutation::set_shipping_zone_method_enabled( $zone_id, $instance_id, $method, $input['enabled'] );
×
149
                        }
150

151
                        /**
152
                         * Filter shipping method object before responding.
153
                         *
154
                         * @param \WC_Shipping_Method $method  The shipping method object.
155
                         * @param \WC_Shipping_Zone   $zone    The shipping zone object.
156
                         * @param array               $input   Request input.
157
                         */
158
                        $method = apply_filters( 'graphql_woocommerce_shipping_zone_method_update', $method, $zone, $input );
1✔
159

160
                        return [
1✔
161
                                'zone_id' => $zone_id,
1✔
162
                                'zone'    => $zone,
1✔
163
                                'method'  => $method,
1✔
164
                        ];
1✔
165
                };
138✔
166
        }
167
}
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