• 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

93.65
/includes/mutation/class-shipping-zone-method-remove.php
1
<?php
2
/**
3
 * Mutation - removeMethodFromShippingZone
4
 *
5
 * Registers mutation for removing a shipping method from 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\Model\Shipping_Method;
17

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

38
        /**
39
         * Defines the mutation input field configuration
40
         *
41
         * @return array
42
         */
43
        public static function get_input_fields() {
44
                return [
138✔
45
                        'zoneId'     => [
138✔
46
                                'type'        => [ 'non_null' => 'Int' ],
138✔
47
                                'description' => __( 'The ID of the shipping zone to delete.', 'wp-graphql-woocommerce' ),
138✔
48
                        ],
138✔
49
                        'instanceId' => [
138✔
50
                                'type'        => [ 'non_null' => 'Int' ],
138✔
51
                                'description' => __( 'Shipping method instance ID', 'wp-graphql-woocommerce' ),
138✔
52
                        ],
138✔
53
                ];
138✔
54
        }
55

56
        /**
57
         * Defines the mutation output field configuration
58
         *
59
         * @return array
60
         */
61
        public static function get_output_fields() {
62
                return [
138✔
63
                        'shippingZone'  => [
138✔
64
                                'type'    => 'ShippingZone',
138✔
65
                                'resolve' => static function ( $payload, array $args, AppContext $context ) {
138✔
66
                                        return $context->get_loader( 'shipping_zone' )->load( $payload['zone_id'] );
1✔
67
                                },
138✔
68
                        ],
138✔
69
                        'removedMethod' => [
138✔
70
                                'type'    => 'ShippingZoneToShippingMethodConnectionEdge',
138✔
71
                                'resolve' => static function ( $payload, array $args, AppContext $context ) {
138✔
72
                                        return [
1✔
73
                                                // Call the Shipping_Method constructor directly because "$payload['method']" is a non-scalar value.
74
                                                'node'   => new Shipping_Method( $payload['method'] ),
1✔
75
                                                'source' => $context->get_loader( 'shipping_zone' )->load( $payload['zone_id'] ),
1✔
76
                                        ];
1✔
77
                                },
138✔
78
                        ],
138✔
79
                ];
138✔
80
        }
81

82
        /**
83
         * Defines the mutation data modification closure.
84
         *
85
         * @return callable
86
         */
87
        public static function mutate_and_get_payload() {
88
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
138✔
89
                        if ( ! \wc_shipping_enabled() ) {
1✔
NEW
90
                                throw new UserError( __( 'Shipping is disabled.', 'wp-graphql-woocommerce' ), 404 );
×
91
                        }
92

93
                        if ( ! \wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
1✔
94
                                throw new UserError( __( 'Sorry, you are not allowed to remove shipping methods', 'wp-graphql-woocommerce' ), \rest_authorization_required_code() );
1✔
95
                        }
96

97
                        $instance_id = $input['instanceId'];
1✔
98
                        $zone_id     = $input['zoneId'];
1✔
99
                        /** @var \WC_Shipping_Zone|false $zone */
100
                        $zone = \WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id );
1✔
101

102
                        if ( false === $zone ) {
1✔
NEW
103
                                throw new UserError( __( 'Invalid shipping zone ID.', 'wp-graphql-woocommerce' ) );
×
104
                        }
105

106
                        if ( 0 === $zone->get_id() ) {
1✔
NEW
107
                                throw new UserError( __( 'Invalid shipping zone ID.', 'wp-graphql-woocommerce' ) );
×
108
                        }
109

110
                        $methods = $zone->get_shipping_methods();
1✔
111
                        $method  = false;
1✔
112

113
                        foreach ( $methods as $shipping_method ) {
1✔
114
                                if ( $shipping_method->instance_id === $instance_id ) {
1✔
115
                                        $method = $shipping_method;
1✔
116
                                        break;
1✔
117
                                }
118
                        }
119

120
                        if ( ! $method ) {
1✔
NEW
121
                                throw new UserError( __( 'Invalid shipping method instance ID.', 'wp-graphql-woocommerce' ) );
×
122
                        }
123

124
                        /**
125
                         * Filter shipping method object before it's removed from the shipping zone.
126
                         *
127
                         * @param \WC_Shipping_Method $method  The shipping method to be deleted.
128
                         * @param \WC_Shipping_Zone   $zone    The shipping zone object.
129
                         * @param array               $input   Request input.
130
                         */
131
                        $method = apply_filters( 'graphql_woocommerce_shipping_zone_method_add', $method, $zone, $input );
1✔
132

133
                        $zone->delete_shipping_method( $instance_id );
1✔
134

135
                        return [
1✔
136
                                'zone_id' => $zone_id,
1✔
137
                                'zone'    => $zone,
1✔
138
                                'method'  => $method,
1✔
139
                        ];
1✔
140
                };
138✔
141
        }
142
}
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