feat: Add zone management backend + drawer z-index fix + SettingsCard action prop
## 1. Fixed Drawer Z-Index ✅ - Increased drawer z-index from 50 to 60 - Now appears above bottom navigation (z-50) - Fixes mobile drawer visibility issue ## 2. Zone Management Backend ✅ Added full CRUD for shipping zones: - POST /settings/shipping/zones - Create zone - PUT /settings/shipping/zones/{id} - Update zone - DELETE /settings/shipping/zones/{id} - Delete zone - GET /settings/shipping/locations - Get countries/states/continents Features: - Create zones with name and regions - Update zone name and regions - Delete zones - Region selector with continents, countries, and states - Proper cache invalidation ## 3. Zone Management Frontend (In Progress) ⏳ - Added state for zone CRUD (showAddZone, editingZone, deletingZone) - Added mutations (createZone, updateZone, deleteZone) - Added "Add Zone" button to SettingsCard - Updated empty state with "Create First Zone" button ## 4. Enhanced SettingsCard Component ✅ - Added optional `action` prop for header buttons - Flexbox layout for title/description + action - Used in Shipping zones for "Add Zone" button ## Next Steps: - Add delete button to each zone - Create Add/Edit Zone dialog with region selector - Add delete confirmation dialog - Then move to Tax rates and Email subjects
This commit is contained in:
@@ -30,6 +30,60 @@ class ShippingController extends WP_REST_Controller {
|
||||
'callback' => array( $this, 'get_zones' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
),
|
||||
array(
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'create_zone' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
'args' => array(
|
||||
'name' => array(
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
),
|
||||
'regions' => array(
|
||||
'required' => false,
|
||||
'type' => 'array',
|
||||
'default' => array(),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// Update/Delete specific zone
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/zones/(?P<zone_id>\d+)',
|
||||
array(
|
||||
array(
|
||||
'methods' => \WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_zone' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
'args' => array(
|
||||
'zone_id' => array(
|
||||
'required' => true,
|
||||
'type' => 'integer',
|
||||
),
|
||||
'name' => array(
|
||||
'required' => false,
|
||||
'type' => 'string',
|
||||
),
|
||||
'regions' => array(
|
||||
'required' => false,
|
||||
'type' => 'array',
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => \WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_zone' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
'args' => array(
|
||||
'zone_id' => array(
|
||||
'required' => true,
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -144,6 +198,19 @@ class ShippingController extends WP_REST_Controller {
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// Get available countries and states for zone regions
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/locations',
|
||||
array(
|
||||
array(
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_locations' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -676,6 +743,193 @@ class ShippingController extends WP_REST_Controller {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new shipping zone
|
||||
*/
|
||||
public function create_zone( WP_REST_Request $request ) {
|
||||
$name = sanitize_text_field( $request->get_param( 'name' ) );
|
||||
$regions = $request->get_param( 'regions' ) ?: array();
|
||||
|
||||
try {
|
||||
$zone = new \WC_Shipping_Zone( null );
|
||||
$zone->set_zone_name( $name );
|
||||
$zone->set_zone_order( 0 );
|
||||
$zone->save();
|
||||
|
||||
// Add regions/locations
|
||||
if ( ! empty( $regions ) ) {
|
||||
foreach ( $regions as $region ) {
|
||||
$zone->add_location( $region['code'], $region['type'] );
|
||||
}
|
||||
$zone->save();
|
||||
}
|
||||
|
||||
// Clear cache
|
||||
\WC_Cache_Helper::invalidate_cache_group( 'shipping_zones' );
|
||||
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'success' => true,
|
||||
'zone_id' => $zone->get_id(),
|
||||
'message' => __( 'Shipping zone created successfully', 'woonoow' ),
|
||||
),
|
||||
201
|
||||
);
|
||||
} catch ( \Exception $e ) {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'error' => 'create_failed',
|
||||
'message' => $e->getMessage(),
|
||||
),
|
||||
500
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a shipping zone
|
||||
*/
|
||||
public function update_zone( WP_REST_Request $request ) {
|
||||
$zone_id = $request->get_param( 'zone_id' );
|
||||
$name = $request->get_param( 'name' );
|
||||
$regions = $request->get_param( 'regions' );
|
||||
|
||||
try {
|
||||
$zone = \WC_Shipping_Zones::get_zone( $zone_id );
|
||||
if ( ! $zone || ! $zone->get_id() ) {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'error' => 'zone_not_found',
|
||||
'message' => __( 'Shipping zone not found', 'woonoow' ),
|
||||
),
|
||||
404
|
||||
);
|
||||
}
|
||||
|
||||
if ( $name ) {
|
||||
$zone->set_zone_name( sanitize_text_field( $name ) );
|
||||
}
|
||||
|
||||
if ( $regions !== null ) {
|
||||
// Clear existing locations
|
||||
$zone->clear_locations();
|
||||
|
||||
// Add new locations
|
||||
foreach ( $regions as $region ) {
|
||||
$zone->add_location( $region['code'], $region['type'] );
|
||||
}
|
||||
}
|
||||
|
||||
$zone->save();
|
||||
|
||||
// Clear cache
|
||||
\WC_Cache_Helper::invalidate_cache_group( 'shipping_zones' );
|
||||
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'success' => true,
|
||||
'message' => __( 'Shipping zone updated successfully', 'woonoow' ),
|
||||
),
|
||||
200
|
||||
);
|
||||
} catch ( \Exception $e ) {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'error' => 'update_failed',
|
||||
'message' => $e->getMessage(),
|
||||
),
|
||||
500
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a shipping zone
|
||||
*/
|
||||
public function delete_zone( WP_REST_Request $request ) {
|
||||
$zone_id = $request->get_param( 'zone_id' );
|
||||
|
||||
try {
|
||||
$zone = \WC_Shipping_Zones::get_zone( $zone_id );
|
||||
if ( ! $zone || ! $zone->get_id() ) {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'error' => 'zone_not_found',
|
||||
'message' => __( 'Shipping zone not found', 'woonoow' ),
|
||||
),
|
||||
404
|
||||
);
|
||||
}
|
||||
|
||||
$zone->delete();
|
||||
|
||||
// Clear cache
|
||||
\WC_Cache_Helper::invalidate_cache_group( 'shipping_zones' );
|
||||
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'success' => true,
|
||||
'message' => __( 'Shipping zone deleted successfully', 'woonoow' ),
|
||||
),
|
||||
200
|
||||
);
|
||||
} catch ( \Exception $e ) {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'error' => 'delete_failed',
|
||||
'message' => $e->getMessage(),
|
||||
),
|
||||
500
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available countries and states for zone regions
|
||||
*/
|
||||
public function get_locations( WP_REST_Request $request ) {
|
||||
$countries_obj = new \WC_Countries();
|
||||
$countries = $countries_obj->get_countries();
|
||||
$states = $countries_obj->get_states();
|
||||
|
||||
$locations = array();
|
||||
|
||||
// Add continents
|
||||
$continents = $countries_obj->get_continents();
|
||||
foreach ( $continents as $code => $continent ) {
|
||||
$locations[] = array(
|
||||
'type' => 'continent',
|
||||
'code' => $code,
|
||||
'name' => $continent['name'],
|
||||
'label' => sprintf( '%s (Continent)', $continent['name'] ),
|
||||
);
|
||||
}
|
||||
|
||||
// Add countries
|
||||
foreach ( $countries as $code => $name ) {
|
||||
$locations[] = array(
|
||||
'type' => 'country',
|
||||
'code' => $code,
|
||||
'name' => $name,
|
||||
'label' => $name,
|
||||
);
|
||||
|
||||
// Add states for this country if available
|
||||
if ( isset( $states[ $code ] ) && ! empty( $states[ $code ] ) ) {
|
||||
foreach ( $states[ $code ] as $state_code => $state_name ) {
|
||||
$locations[] = array(
|
||||
'type' => 'state',
|
||||
'code' => $code . ':' . $state_code,
|
||||
'name' => $state_name,
|
||||
'label' => sprintf( '%s — %s', $name, $state_name ),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new WP_REST_Response( $locations, 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has permission to manage shipping
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user