2024-05-17 00:03:10 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - updateMethodOnShippingZone
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for update a shipping method on a shipping zone.
|
|
|
|
|
*
|
|
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
2024-05-21 00:57:56 -04:00
|
|
|
* @since 0.20.0
|
2024-05-17 00:03:10 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
|
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
|
|
|
|
use WPGraphQL\WooCommerce\Data\Mutation\Shipping_Mutation;
|
|
|
|
|
use WPGraphQL\WooCommerce\Model\Shipping_Method;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class - Shipping_Zone_Method_Update
|
|
|
|
|
*/
|
|
|
|
|
class Shipping_Zone_Method_Update {
|
|
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
|
|
|
|
'updateMethodOnShippingZone',
|
|
|
|
|
[
|
|
|
|
|
'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 [
|
|
|
|
|
'zoneId' => [
|
|
|
|
|
'type' => [ 'non_null' => 'Int' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The ID of the shipping zone to delete.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2024-05-17 00:03:10 -04:00
|
|
|
],
|
|
|
|
|
'instanceId' => [
|
|
|
|
|
'type' => [ 'non_null' => 'Int' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Shipping method instance ID', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2024-05-17 00:03:10 -04:00
|
|
|
],
|
|
|
|
|
'enabled' => [
|
|
|
|
|
'type' => 'Boolean',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Whether the shipping method is enabled or not.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2024-05-17 00:03:10 -04:00
|
|
|
],
|
|
|
|
|
'order' => [
|
|
|
|
|
'type' => 'Int',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The order of the shipping method.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2024-05-17 00:03:10 -04:00
|
|
|
],
|
|
|
|
|
'settings' => [
|
|
|
|
|
'type' => [ 'list_of' => 'WCSettingInput' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The settings for the shipping method.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2024-05-17 00:03:10 -04:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation output field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_output_fields() {
|
|
|
|
|
return [
|
|
|
|
|
'shippingZone' => [
|
|
|
|
|
'type' => 'ShippingZone',
|
|
|
|
|
'resolve' => static function ( $payload, array $args, AppContext $context ) {
|
|
|
|
|
return $context->get_loader( 'shipping_zone' )->load( $payload['zone_id'] );
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
'method' => [
|
|
|
|
|
'type' => 'ShippingZoneToShippingMethodConnectionEdge',
|
|
|
|
|
'resolve' => static function ( $payload, array $args, AppContext $context ) {
|
|
|
|
|
return [
|
|
|
|
|
// Call the Shipping_Method constructor directly because "$payload['method']" is a non-scalar value.
|
|
|
|
|
'node' => new Shipping_Method( $payload['method'] ),
|
|
|
|
|
'source' => $context->get_loader( 'shipping_zone' )->load( $payload['zone_id'] ),
|
|
|
|
|
];
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation data modification closure.
|
|
|
|
|
*
|
|
|
|
|
* @return callable
|
|
|
|
|
*/
|
|
|
|
|
public static function mutate_and_get_payload() {
|
|
|
|
|
return static function ( $input, AppContext $context, ResolveInfo $info ) {
|
|
|
|
|
if ( ! \wc_shipping_enabled() ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Shipping is disabled.', 'graphql-for-ecommerce' ), 404 );
|
2024-05-17 00:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! \wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Sorry, you are not allowed to edit shipping methods.', 'graphql-for-ecommerce' ), \rest_authorization_required_code() );
|
2024-05-17 00:03:10 -04:00
|
|
|
}
|
|
|
|
|
$instance_id = $input['instanceId'];
|
|
|
|
|
$zone_id = $input['zoneId'];
|
|
|
|
|
/** @var \WC_Shipping_Zone|false $zone */
|
|
|
|
|
$zone = \WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id );
|
|
|
|
|
|
|
|
|
|
if ( false === $zone ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Invalid shipping zone ID.', 'graphql-for-ecommerce' ) );
|
2024-05-17 00:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( 0 === $zone->get_id() ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Invalid shipping zone ID.', 'graphql-for-ecommerce' ) );
|
2024-05-17 00:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$methods = $zone->get_shipping_methods();
|
|
|
|
|
$method = false;
|
|
|
|
|
|
|
|
|
|
foreach ( $methods as $shipping_method ) {
|
|
|
|
|
if ( $shipping_method->instance_id === $instance_id ) {
|
|
|
|
|
$method = $shipping_method;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! $method ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Invalid shipping method instance ID.', 'graphql-for-ecommerce' ) );
|
2024-05-17 00:03:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update settings.
|
|
|
|
|
if ( ! empty( $input['settings'] ) ) {
|
|
|
|
|
$method = Shipping_Mutation::set_shipping_zone_method_settings( $instance_id, $method, $input['settings'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update order.
|
|
|
|
|
if ( isset( $input['order'] ) ) {
|
|
|
|
|
$method = Shipping_Mutation::set_shipping_zone_method_order( $instance_id, $method, $input['order'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update if this method is enabled or not.
|
|
|
|
|
if ( isset( $input['enabled'] ) ) {
|
|
|
|
|
$method = Shipping_Mutation::set_shipping_zone_method_enabled( $zone_id, $instance_id, $method, $input['enabled'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filter shipping method object before responding.
|
|
|
|
|
*
|
|
|
|
|
* @param \WC_Shipping_Method $method The shipping method object.
|
|
|
|
|
* @param \WC_Shipping_Zone $zone The shipping zone object.
|
|
|
|
|
* @param array $input Request input.
|
|
|
|
|
*/
|
|
|
|
|
$method = apply_filters( 'graphql_woocommerce_shipping_zone_method_update', $method, $zone, $input );
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'zone_id' => $zone_id,
|
|
|
|
|
'zone' => $zone,
|
|
|
|
|
'method' => $method,
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|