WooCommerce offers a streamlined shipping address system, enabling customers to complete a shipping address form, which is then automatically stored in the database. This process includes capturing essential details like the postcode (zip code), city, and additional relevant information.
In this post, we provide concise codes for WooCommerce, featuring built-in validation that aids in the identification of suitable shipping methods during the checkout process. Furthermore, we delve into the functionality of WooCommerce, specifically highlighting how it can echo the address details, such as the current address pin-code and city zip code, from the login user’s profile.
<?php
$customer_id = get_current_user_id();
if ( ! wc_ship_to_billing_address_only() && wc_shipping_enabled() ) {
$get_addresses = apply_filters(
'woocommerce_my_account_get_addresses',
array(
'shipping' => __( 'Shipping address', 'woocommerce' ),
),
$customer_id
);
}
?>
<?php foreach ( $get_addresses as $name => $address_title ) : ?>
<?php
$address = wc_get_account_formatted_address( $name );
?>
<?php endforeach; ?>
<?php
echo get_user_meta( $customer_id, $name . '_postcode', true )."<br>";
echo get_user_meta( $customer_id, $name . '_city', true );
?>
This code specifically retrieves the shipping address, postal code (ZIP code), and city of the logged-in user, then promptly echoes the details in a direct format, resembling something like (110001 and Delhi). The information acquired comprises the entire range of shipping zone data, which encompasses both the postcode and city.
This accomplishment is facilitated by utilizing the ‘customer_id’ as the object identifier and the ‘billing address’ as a basis for comparison within the ‘my account’ context. The rationale behind this approach lies in the fact that the SQL query yields an array of objects, each possessing these particular attributes.