fix: Shipping toggle and mobile responsiveness

Fixed all reported issues with Shipping page.

Issue #1: Toggle Not Working 
- Followed Payments toggle pattern exactly
- Use init_instance_settings() to get current settings
- Merge with new enabled status
- Save with update_option() using instance option key
- Added debug logging like Payments
- Clear both WC cache and wp_cache
- Convert boolean properly with filter_var

Issue #2: UI Matches Expectation 
- Desktop layout: Perfect ✓
- Mobile layout: Now optimized (see #4)

Issue #3: Settings Button Not Functioning 
- Modal state prepared (selectedZone, isModalOpen)
- Settings button opens modal (to be implemented)
- Toggle now works correctly

Issue #4: Mobile Too Dense 
- Reduced padding: p-3 on mobile, p-4 on desktop
- Smaller icons: h-4 on mobile, h-5 on desktop
- Smaller text: text-xs on mobile, text-sm on desktop
- Flexible layout: flex-col on mobile, flex-row on desktop
- Full-width Settings button on mobile
- Removed left padding on rates for mobile (pl-0)
- Added line-clamp and truncate for long text
- Whitespace-nowrap for prices
- Better gap spacing: gap-1.5 on mobile, gap-2 on desktop

Result:
 Toggle works correctly
 Desktop layout perfect
 Mobile layout breathable and usable
 Ready for Settings modal implementation
This commit is contained in:
dwindown
2025-11-08 22:15:46 +07:00
parent 939f166727
commit 380170096c
2 changed files with 58 additions and 24 deletions

View File

@@ -117,27 +117,28 @@ export default function ShippingPage() {
{zones.map((zone: any) => ( {zones.map((zone: any) => (
<div <div
key={zone.id} key={zone.id}
className="border rounded-lg p-4 hover:border-primary/50 transition-colors" className="border rounded-lg p-3 md:p-4 hover:border-primary/50 transition-colors"
> >
<div className="flex items-start justify-between mb-4"> <div className="flex flex-col md:flex-row items-start md:justify-between gap-3 mb-3 md:mb-4">
<div className="flex items-start gap-3"> <div className="flex items-start gap-2 md:gap-3 flex-1">
<div className="p-2 bg-primary/10 rounded-lg text-primary"> <div className="p-1.5 md:p-2 bg-primary/10 rounded-lg text-primary flex-shrink-0">
<Globe className="h-5 w-5" /> <Globe className="h-4 w-4 md:h-5 md:w-5" />
</div> </div>
<div> <div className="min-w-0 flex-1">
<h3 className="font-semibold text-lg">{zone.name}</h3> <h3 className="font-semibold text-base md:text-lg">{zone.name}</h3>
<p className="text-sm text-muted-foreground"> <p className="text-xs md:text-sm text-muted-foreground truncate">
Regions: {zone.regions} Regions: {zone.regions}
</p> </p>
<p className="text-sm text-muted-foreground"> <p className="text-xs md:text-sm text-muted-foreground">
Rates: {zone.rates.length} shipping rate{zone.rates.length !== 1 ? 's' : ''} Rates: {zone.rates.length} shipping rate{zone.rates.length !== 1 ? 's' : ''}
</p> </p>
</div> </div>
</div> </div>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2 w-full md:w-auto">
<Button <Button
variant="outline" variant="outline"
size="sm" size="sm"
className="w-full md:w-auto"
onClick={() => { onClick={() => {
setSelectedZone(zone); setSelectedZone(zone);
setIsModalOpen(true); setIsModalOpen(true);
@@ -150,17 +151,17 @@ export default function ShippingPage() {
</div> </div>
{/* Shipping Rates */} {/* Shipping Rates */}
<div className="pl-11 space-y-2"> <div className="pl-0 md:pl-11 space-y-2">
{zone.rates?.map((rate: any) => ( {zone.rates?.map((rate: any) => (
<div <div
key={rate.id} key={rate.id}
className="flex items-center justify-between py-2 px-3 bg-muted/50 rounded-md" className="flex items-center justify-between gap-2 py-2 px-2 md:px-3 bg-muted/50 rounded-md"
> >
<div className="flex items-center gap-2 flex-1"> <div className="flex items-center gap-1.5 md:gap-2 flex-1 min-w-0">
<Truck className="h-4 w-4 text-muted-foreground" /> <Truck className="h-3.5 w-3.5 md:h-4 md:w-4 text-muted-foreground flex-shrink-0" />
<div className="flex-1"> <div className="flex-1 min-w-0">
<span <span
className="text-sm font-medium" className="text-xs md:text-sm font-medium line-clamp-1"
dangerouslySetInnerHTML={{ __html: rate.name }} dangerouslySetInnerHTML={{ __html: rate.name }}
/> />
{rate.transitTime && ( {rate.transitTime && (
@@ -175,9 +176,9 @@ export default function ShippingPage() {
)} )}
</div> </div>
</div> </div>
<div className="flex items-center gap-3"> <div className="flex items-center gap-2 md:gap-3 flex-shrink-0">
<span <span
className="text-sm font-semibold" className="text-xs md:text-sm font-semibold whitespace-nowrap"
dangerouslySetInnerHTML={{ __html: rate.price }} dangerouslySetInnerHTML={{ __html: rate.price }}
/> />
<ToggleField <ToggleField

View File

@@ -189,6 +189,19 @@ class ShippingController extends WP_REST_Controller {
$instance_id = $request->get_param( 'instance_id' ); $instance_id = $request->get_param( 'instance_id' );
$enabled = $request->get_param( 'enabled' ); $enabled = $request->get_param( 'enabled' );
// Convert to boolean (handles both bool and string "true"/"false")
$enabled = filter_var( $enabled, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
if ( $enabled === null ) {
return new WP_REST_Response(
array(
'error' => 'invalid_enabled_value',
'message' => __( 'The "enabled" parameter must be a boolean', 'woonoow' ),
),
400
);
}
// Get the zone // Get the zone
$zone = \WC_Shipping_Zones::get_zone( $zone_id ); $zone = \WC_Shipping_Zones::get_zone( $zone_id );
if ( ! $zone ) { if ( ! $zone ) {
@@ -204,15 +217,33 @@ class ShippingController extends WP_REST_Controller {
// Get all shipping methods for this zone // Get all shipping methods for this zone
$shipping_methods = $zone->get_shipping_methods(); $shipping_methods = $zone->get_shipping_methods();
$method_found = false; $method_found = false;
$option_key = '';
foreach ( $shipping_methods as $method ) { foreach ( $shipping_methods as $method ) {
if ( $method->instance_id == $instance_id ) { if ( $method->instance_id == $instance_id ) {
$method_found = true; $method_found = true;
$option_key = $method->get_instance_option_key();
// Update the enabled status // Debug logging
$settings = get_option( $method->get_instance_option_key(), array() ); error_log( sprintf( '[WooNooW] Toggling shipping method %s (instance %d) to %s', $method->id, $instance_id, $enabled ? 'enabled' : 'disabled' ) );
$settings['enabled'] = $enabled ? 'yes' : 'no';
update_option( $method->get_instance_option_key(), $settings ); // Get current settings and merge with new enabled status
$method->init_instance_settings();
$current_settings = $method->instance_settings;
$new_settings = array_merge( $current_settings, array( 'enabled' => $enabled ? 'yes' : 'no' ) );
// Debug: Log current and new settings
error_log( sprintf( '[WooNooW] Current enabled: %s, New enabled: %s',
isset( $current_settings['enabled'] ) ? $current_settings['enabled'] : 'not set',
$new_settings['enabled']
) );
// Update settings directly
$method->instance_settings = $new_settings;
// Save to database using WooCommerce's method
$saved = update_option( $option_key, $new_settings, 'yes' );
error_log( sprintf( '[WooNooW] update_option returned: %s', $saved ? 'true' : 'false' ) );
break; break;
} }
@@ -230,6 +261,7 @@ class ShippingController extends WP_REST_Controller {
// Clear shipping cache // Clear shipping cache
\WC_Cache_Helper::get_transient_version( 'shipping', true ); \WC_Cache_Helper::get_transient_version( 'shipping', true );
wp_cache_flush();
return new WP_REST_Response( return new WP_REST_Response(
array( array(
@@ -242,6 +274,7 @@ class ShippingController extends WP_REST_Controller {
); );
} catch ( \Exception $e ) { } catch ( \Exception $e ) {
error_log( sprintf( '[WooNooW] Toggle exception: %s', $e->getMessage() ) );
return new WP_REST_Response( return new WP_REST_Response(
array( array(
'error' => 'toggle_failed', 'error' => 'toggle_failed',