WooCommerce Default Shipping Zip Code

I am trying to pre-populate the WooCommerce shipping address with customer data. This is working for all fields except the shipping_postcode. Any ideas what I am doing wrong?

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
    $customer_id = get_current_user_id();
    $fields['shipping']['shipping_first_name']['default'] = get_user_meta( $customer_id, $name . 'shipping_first_name', true );
    $fields['shipping']['shipping_last_name']['default'] = get_user_meta( $customer_id, $name . 'shipping_last_name', true );
    $fields['shipping']['shipping_company']['default'] = get_user_meta( $customer_id, $name . 'shipping_company', true );
    $fields['shipping']['shipping_address_1']['default'] = get_user_meta( $customer_id, $name . 'shipping_address_1', true );
    $fields['shipping']['shipping_address_2']['default'] = get_user_meta( $customer_id, $name . 'shipping_address_2', true );
    $fields['shipping']['shipping_city']['default'] = get_user_meta( $customer_id, $name . 'shipping_city', true );
    $fields['shipping']['shipping_postcode']['default'] = get_user_meta( $customer_id, $name . 'shipping_postcode', true );
    $fields['shipping']['shipping_country']['default'] = get_user_meta( $customer_id, $name . 'shipping_country', true );
    $fields['shipping']['shipping_state']['default'] = get_user_meta( $customer_id, $name . 'shipping_state', true );
    return $fields;
}

I have even tried a simple assignment with no luck:

    $fields['shipping']['shipping_postcode']['default'] = "90210";

Related posts

Leave a Reply

1 comment

  1. We found a workaround for this.

    This line for some reason does not work:

    $fields['shipping']['shipping_postcode']['default'] = get_user_meta( $customer_id, $name . 'shipping_postcode', true );
    

    But this code accomplishes what we want:

    $woocommerce->customer->set_shipping_postcode(get_user_meta( $customer_id, $name . 'shipping_postcode', true ));