feat: Add toggle functionality to Shipping methods
Implemented inline enable/disable for shipping methods. Frontend Changes: ✅ Allow HTML in shipping method names and prices ✅ Add toggle switches to each shipping method ✅ Loading state while toggling ✅ Toast notifications for success/error ✅ Optimistic UI updates via React Query Backend Changes: ✅ POST /settings/shipping/zones/{zone_id}/methods/{instance_id}/toggle ✅ Enable/disable shipping methods ✅ Clear WooCommerce shipping cache ✅ Proper error handling User Experience: - Quick enable/disable without leaving page - Similar to Payments page pattern - Complex configuration still in WooCommerce - Edit zone button for detailed settings - Add zone button for new zones Result: ✅ Functional shipping management ✅ No need to redirect for simple toggles ✅ Maintains WooCommerce compatibility ✅ Clean, intuitive interface
This commit is contained in:
@@ -32,6 +32,33 @@ class ShippingController extends WP_REST_Controller {
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// Toggle shipping method
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/zones/(?P<zone_id>\d+)/methods/(?P<instance_id>\d+)/toggle',
|
||||
array(
|
||||
array(
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'toggle_method' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
'args' => array(
|
||||
'zone_id' => array(
|
||||
'required' => true,
|
||||
'type' => 'integer',
|
||||
),
|
||||
'instance_id' => array(
|
||||
'required' => true,
|
||||
'type' => 'integer',
|
||||
),
|
||||
'enabled' => array(
|
||||
'required' => true,
|
||||
'type' => 'boolean',
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,6 +180,77 @@ class ShippingController extends WP_REST_Controller {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle shipping method enabled/disabled
|
||||
*/
|
||||
public function toggle_method( WP_REST_Request $request ) {
|
||||
try {
|
||||
$zone_id = $request->get_param( 'zone_id' );
|
||||
$instance_id = $request->get_param( 'instance_id' );
|
||||
$enabled = $request->get_param( 'enabled' );
|
||||
|
||||
// Get the zone
|
||||
$zone = \WC_Shipping_Zones::get_zone( $zone_id );
|
||||
if ( ! $zone ) {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'error' => 'zone_not_found',
|
||||
'message' => __( 'Shipping zone not found', 'woonoow' ),
|
||||
),
|
||||
404
|
||||
);
|
||||
}
|
||||
|
||||
// Get all shipping methods for this zone
|
||||
$shipping_methods = $zone->get_shipping_methods();
|
||||
$method_found = false;
|
||||
|
||||
foreach ( $shipping_methods as $method ) {
|
||||
if ( $method->instance_id == $instance_id ) {
|
||||
$method_found = true;
|
||||
|
||||
// Update the enabled status
|
||||
$method->enabled = $enabled ? 'yes' : 'no';
|
||||
update_option( $method->get_instance_option_key(), $method->instance_settings );
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $method_found ) {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'error' => 'method_not_found',
|
||||
'message' => __( 'Shipping method not found', 'woonoow' ),
|
||||
),
|
||||
404
|
||||
);
|
||||
}
|
||||
|
||||
// Clear shipping cache
|
||||
\WC_Cache_Helper::get_transient_version( 'shipping', true );
|
||||
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'success' => true,
|
||||
'message' => $enabled
|
||||
? __( 'Shipping method enabled', 'woonoow' )
|
||||
: __( 'Shipping method disabled', 'woonoow' ),
|
||||
),
|
||||
200
|
||||
);
|
||||
|
||||
} catch ( \Exception $e ) {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'error' => 'toggle_failed',
|
||||
'message' => $e->getMessage(),
|
||||
),
|
||||
500
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has permission to manage shipping
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user