Magento: Configuring Cities as Dropdown Options for Shipping/Billing
Hello developers! Today we’re going to tackle an interesting challenge in custom Magento development - adding dropdown lists for cities fields in the shipping and billing forms. I’ll provide a step-by-step guide, code examples, and some best practices to help you achieve this.
The Problem: City Dropdown for Address Form
You’ve probably noticed that Magento provides dropdown options for countries and regions (states) by default. However, adding a similar dropdown for cities requires manual configuration and coding. Let’s dive into the solution.
Step 1: Create the Cities Table
Before we start, ensure you have a table to store your city data. If not, create a new table named cities with the following schema:
CREATE TABLE `cities` (
`id_city` INT(10) UNSIGNED AUTO_INCREMENT,
`id_region` INT(10),
`city_name` VARCHAR(255),
PRIMARY KEY ID_CITY (ID_CITY)
);
This table should be linked to the existing regions table via the id_region column.
Step 2: Populate the Cities Table
Populate your new cities table with data relevant to each region. You can do this manually or use an import tool.
Step 3: Modify the Shipping and Billing PHTML Files
Next, you need to modify the shipping.phtml and billing.phtml files located in app/design/frontend/default/yourtheme/template/persistent/customer/form/address.phtml.
Search for the following line:
<div class="field city required-entry">
<label for="city" class="required"><em>*</em><?php echo $this->__('City') ?></label>
<div class="input-box">
<input type="text" name="city" id="city" title="<?php echo $this->__('City') ?>" value="" class="input-text required-entry" />
</div>
</div>
Replace it with:
<div class="field city required-entry">
<label for="city_id" class="required"><em>*</em><?php echo $this->__('City') ?></label>
<div class="input-box">
<select id="city_id" name="city_id" title="<?php echo $this->__('City') ?>" class="input-select required-entry">
<?php foreach ($this->getCities() as $city): ?>
<option value="<?php echo $city['id_city'] ?>"><?php echo $city['city_name'] ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
In this code, $this->getCities() is a custom method that retrieves the cities based on the selected region. We’ll implement this next.
Step 4: Implement the Custom Method
Open app/code/core/Mage/Customer/Model/Address/Abstract.php and add the following method:
public function getCities()
{
$collection = Mage::getModel('your_module/city')->getCollection();
if ($this->getRegionId()) {
$collection->addFieldToFilter('id_region', array('eq' => $this->getRegionId()));
}
return $collection;
}
This method retrieves the cities collection based on the region ID.
Step 5: Update the Address Model
Create a new module named Your_Module and add an entity called City. The city model should extend Mage_Eav_Model_Entity_Abstract. Follow this guide for detailed steps.
Conclusion
That’s it! You’ve successfully configured your Magento site to display cities as a dropdown option in the shipping and billing forms. Remember to flush the cache after making changes.
Feel free to explore LaravelCompany for more in-depth tutorials on Magento development, including how to create new EAV entities, customize checkout processes, and optimize performance.
Happy coding!