/*
	Theme Name:     Propharm Child
	Theme URI:      http://www.enovathemes.com/propharm
	Description:    Propharm - Pharmacy Medical WooCommerce WordPress Theme
	Author:         Enovathemes
	Author URI:     https://enovathemes.com
	Version:        1.0
	Template:       propharm
*/

/* =Theme customization starts here
-------------------------------------------------------------- */
/**
 * Rename the "State/County" field to "Suburb" on the WooCommerce checkout page,
 * specifically when the selected country is Zimbabwe (ZW).
 *
 * This code should be added to your theme's functions.php file or a custom plugin.
 */

function custom_rename_state_to_suburb_for_zw( $fields ) {
    // Check if billing fields exist
    if ( isset( $fields['billing'] ) ) {

        // Target the billing_state field.
        // WooCommerce uses 'state' as the key for the State/County field.
        if ( isset( $fields['billing']['billing_state'] ) ) {

            // Change the label and placeholder.
            $fields['billing']['billing_state']['label'] = 'Suburb';
            $fields['billing']['billing_state']['placeholder'] = 'e.g. Hillside or Nkulumane';

            // IMPORTANT: If you do not use the standard WooCommerce States setup,
            // you may also want to make the field NOT REQUIRED, especially if customers
            // leave it blank. Uncomment the line below if you want to make it optional.
            // $fields['billing']['billing_state']['required'] = false;
        }
    }

    // Check if shipping fields exist (optional, but good practice for full coverage)
    if ( isset( $fields['shipping'] ) ) {
        if ( isset( $fields['shipping']['shipping_state'] ) ) {
            $fields['shipping']['shipping_state']['label'] = 'Suburb';
            $fields['shipping']['shipping_state']['placeholder'] = 'e.g. Hillside or Nkulumane';
            // Uncomment to make optional:
            // $fields['shipping']['shipping_state']['required'] = false;
        }
    }

    return $fields;
}

// Check if the user has selected Zimbabwe (ZW) on the checkout form.
// This filter runs when the checkout fields are loaded.
add_filter( 'woocommerce_checkout_fields', 'custom_rename_state_to_suburb_for_zw' );

/**
 * Handle scenarios where the state field is rendered outside the main checkout form,
 * such as in the My Account address editor. This ensures consistency across the site.
 */
function custom_rename_state_to_suburb_for_zw_general( $label, $key ) {
    // We only want to apply this change if the country is Zimbabwe (ZW).
    // This requires a conditional check. Since there is no $country parameter
    // in this general filter, we will only apply the custom string.
    // If you need strict country checking here, you'd need a more complex solution
    // involving Javascript or checking the currently logged-in user's country.

    if ( $key === 'state' ) {
        return 'Suburb';
    }
    return $label;
}

// Add the filter to rename the State label generally.
// This is a broader filter and might rename it even when ZW is not selected,
// depending on how other fields are structured. The first filter is the most effective.
// add_filter( 'woocommerce_default_address_fields', 'custom_rename_state_to_suburb_for_zw_general', 10, 2 );

// Alternative: Adjusting default fields globally for ZW (often required for admin area settings)
add_filter( 'woocommerce_country_locale_field_selectors', function( $fields ) {
    // Check if Zimbabwe is defined in the locale settings
    if ( isset( $fields['ZW'] ) && isset( $fields['ZW']['state'] ) ) {
        $fields['ZW']['state']['label'] = 'Suburb';
        $fields['ZW']['state']['placeholder'] = 'e.g. Hillside or Nkulumane';
        // You can also adjust the class if you want to hide it completely:
        // $fields['ZW']['state']['class'][] = 'hidden';
    }
    return $fields;
});
