Magento - Select zip code and address for customer(s)

Magento - Select zip code and address for customer(s)

July 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Select zip code and address for customer(s)

Retrieving Customer Addresses and Zip Codes in Magento

As an experienced technical blogger and senior developer, I often receive queries about retrieving customer data for frontend modules in Magento. In this post, I’ll guide you through the process of accessing a customer’s address and zip code in the Magento 2 frontend.

Understanding the Magento Customer Address Structure

Before we dive into the code, let’s understand how Magento structures customer addresses. Each customer can have multiple addresses associated with their account. These addresses are stored as separate records under the customer_address_entity table in your database.

The relevant fields for our purpose are:

  • entity_id: The unique identifier for each address.
  • parent_id: The ID of the associated customer (the parent).
  • street: An array containing both street lines.
  • postcode: The zip code.
  • city: The city name.
  • country_id: The country ISO code.

Retrieving Customer Addresses via API

Magento provides a RESTful API for accessing customer data, including addresses. To retrieve the address and zip code of a customer, we’ll use the Magento 2 Customer API.

Firstly, ensure you have enabled the Magento API by following these steps:

  1. Log in to your Magento admin panel.
  2. Navigate to Stores > Settings > Configuration > Services > Magento Web APIs.
  3. Set “Web API Enabled for Frontend” and “Web API Enabled for Admin” to Yes.

Next, let’s write a PHP script that retrieves the customer address using their ID:

<?php

// Include your project root path or use Composer autoloading if set up.
require_once 'path/to/magento/root/autoload.php';

$customerId = 1; // Replace with your desired customer ID.

try {
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $apiClient = $objectManager->create('Magento\Customer\Api\AddressRepositoryInterface');

    $addresses = $apiClient->getList($customerId);

    foreach ($addresses as $address) {
        echo "Street: {$address->getStreet(1)}" . PHP_EOL;
        echo "Zip Code: {$address->getPostcode()}" . PHP_EOL;
        // Access other address fields like city, country, etc.
    }
} catch (\Magento\Framework\Exception\LocalizedException $e) {
    echo "Caught exception: " . $e->getMessage();
}

This script retrieves all addresses for the specified customer and prints their street and zip code.

Best Practices and Further Reading

When working with customer data in Magento, it’s essential to follow some best practices:

  • Always validate user input, especially when dealing with sensitive information like addresses.
  • Ensure you have proper access control measures in place to prevent unauthorized access to customer data.
  • Consider using Magento’s built-in security features like CSRF protection and session management.

For a more detailed guide on working with the Magento API, I recommend checking out this comprehensive tutorial by Magento Commerce: Magento 2 Web APIs - An Overview.

Conclusion

In this post, we explored how to retrieve customer addresses and zip codes using the Magento 2 Customer API in a frontend context. By understanding the address structure and leveraging the built-in APIs, you can easily access and manipulate customer data for your custom modules.

Remember to always prioritize security when handling sensitive information like addresses. With proper validation and access control measures, you can create robust, secure solutions that enhance the Magento experience for both customers and administrators.

If you found this guide helpful and would like to delve deeper into Magento development, I invite you to explore our comprehensive suite of Magento resources at Laravel Company. We offer in-depth tutorials, best practices, and expert insights to help you master the Magento platform.

Magento 2 Database API Extensions

Our network