Showing results for 
Search instead for 
Did you mean: 

Estimate Shipping based on city.

Estimate Shipping based on city.



I need to modify the Estimate Shipping and Tax Block to include shipping prices by City as well as postcode and country. Ideally the city field will replace the Region/State field.


I have searched the forum but cannot find a solution to the problem.  


The site is using Magento CE and WebShopApps Premium Rates shipping extension to calculate the costs.


Any help on this matter is much appreciated.






Tags (3)

Re: Estimate Shipping based on city.

Don't know if this is the correct process but it worked for me using Magento 1.9.2.

1. Locate the .phtml file responsible for displaying the html in that block by turning on Template Path Hints. In my case it was located here: 'frontend/theme/default/template/checkout/cart/shipping.phtml'.


2. open the shipping.phtml file and locate line containing this code



           <?php if($this->getCityActive()): ?>
                    <label for="city"<?php if ($this->isCityRequired()) echo ' class="required"' ?>><?php if ($this->isCityRequired()) echo '<em>*</em>' ?><?php echo $this->__('City') ?></label>
                    <div class="input-box">
                        <input class="input-text<?php if ($this->isCityRequired()):?> required-entry<?php endif;?>" id="city" type="text" name="estimate_city" value="<?php echo $this->escapeHtml($this->getEstimateCity()) ?>" />
            <?php endif; ?>

3. Comment first and last lines like this:



           <?//php if($this->getCityActive()): ?>
. <?//php endif; ?>

4. Save file and refresh page. The city field should now be visible. (clear cache if necessary)



Now to remove the Region/State field we have two options:


1. Locate the line with this code (just above the previous code)



            <?php //if($this->getStateActive()): ?>
                    <label for="region_id"<?php if ($this->isStateProvinceRequired()) echo ' class="required"' ?>><?php if ($this->isStateProvinceRequired()) echo '<em>*</em>' ?><?php echo $this->__('State/Province') ?></label>
                    <div class="input-box">
                        <select id="region_id" name="region_id" title="<?php echo Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" style="display:none;"<?php echo ($this->isStateProvinceRequired() ? ' class="validate-select"' : '') ?>>
                            <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                       <script type="text/javascript">
                           $('region_id').setAttribute('defaultValue',  "<?php echo $this->getEstimateRegionId() ?>");
                       <input type="text" id="region" name="region" value="<?php echo $this->escapeHtml($this->getEstimateRegion()) ?>"  title="<?php echo Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="input-text" style="display:none;" />
            <?php //endif; ?>

- Uncomment the fist and last lines to look like below:


            <?php if($this->getStateActive()): ?>
            <?php endif; ?>

- Save file and refresh page. The Region/State field should now be hidden.

Note: this option will only remove Region/State field from the estimate shipping and tax block only but will still appear on other forms to remove completely follow second option.


2. Go to System>>Configuration>>Default in the admin panel and locate the 'state options' drop-down. In 'State is required for' field deselect any country you do not wish to display the Region/State field. Select  No in 'Display not required State'. Save the configuration and changes should take effect.

Re: Estimate Shipping based on city.

Answer: To show the city, enable DHL, but don't enable any allowed methods.

To hide the state, add some javascript to your theme's js file to remove that element from the DOM: $$('#shipping-zip-form .shipping-region')[0].remove(); Don't mark it as not-required, because you do want to collect this from the user on the checkout page. The validation on the estimation form is all client-side, so if you simply remove it from the dom, it won't be required to submit the form. You can also hide it with CSS so that it never shows up (it will take a moment for javascript to remove it.) #shipping-zip-form .shipping-region { display: none; }


The above solution that talks about modifying template files won't be upgrade-safe and forwards-compatible. Nobody wants to manually merge code modifications when Magento releases a new version.


Instead, you can explore where the value of the getCityActive() function is derived from. This function is defined in /app/code/core/Mage/Checkout/Block/Cart/Shipping.php (around like 154 as of Magento

     * Show City in Shipping Estimation
     * @return bool
    public function getCityActive()
        return (bool)Mage::getStoreConfig('carriers/dhl/active')
            || (bool)Mage::getStoreConfig('carriers/dhlint/active');

So if you want to show that city input, just enable DHL, but don't select any allowed shipping methods. This will have the effect of showing the box without actually showing any DHL methods.

The state input shows up in a variety of scenarios, so it isn't as trivial to change with a config value. That's why I'm recommending the CSS/Javascript work-around.