3

Where can I the html select tag for countries on the checkout page?

<div class="field form-group">
       <label for="billing:country_id" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
       <div class="input-box">
          <?php echo $this->getCountryHtmlSelect('billing') ?>
       </div>
</div>

with this $this->getCountryHtmlSelect('billing'), it returns the html select tag below,

<select name="billing[country_id]" id="billing:country_id" class="validate-select" title="Country" >
<option value="" > </option>
<option value="AF" >Afghanistan</option><option value="AL" >Albania</option>
<option value="DZ" >Algeria</option>
...

But I need to add a class name to the select tag,

class="form-control validate-select"

But where can I find this select tag?

1
3

The country select box is prepared by the block,

To change its behavior you just need override the function to you local folder like below.

Just copy the core\Mage\Checkout\Block\Onepage\Billing.php to you local folder like

local\Mage\Checkout\Block\Onepage\Billing.php

Now you just need to put the below code in that file see I have added the "form-control" to setClass() function.

its done now your class will be added to the select box.

public function getCountryHtmlSelect($type)
{
    $countryId = $this->getAddress()->getCountryId();
    if (is_null($countryId)) {
        $countryId = Mage::helper('core')->getDefaultCountry();
    }
    $select = $this->getLayout()->createBlock('core/html_select')
        ->setName($type.'[country_id]')
        ->setId($type.':country_id')
        ->setTitle(Mage::helper('checkout')->__('Country'))
        ->setClass('validate-select form-control')
        ->setValue($countryId)
        ->setOptions($this->getCountryOptions());
    if ($type === 'shipping') {
        $select->setExtraParams('onchange="if(window.shipping)shipping.setSameAsBilling(false);"');
    }

    return $select->getHtml();
}
5

Almost the same as your other question: How to set default shipping method and country?

Try replacing:

<?php echo $this->getCountryHtmlSelect('billing') ?>

With this:

<?php $_countries = Mage::getResourceModel('directory/country_collection')->loadByStore()->toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
    <select name="billing[country_id]" id="billing:country_id" class="validate-select">
        <option value="">Please choose a country...</option>
        <?php foreach($_countries as $_country): ?>
            <option value="<?php echo $_country['value'] ?>">
                <?php echo $_country['label'] ?>
            </option>
        <?php endforeach; ?>
    </select>
<?php endif; ?>

Then all you need to do is add classes to <select name="billing[country_id]" id="billing:country_id" class="validate-select">

1
  • Worked perfectly - no need to overwrite core files or create new - spot on! – Simon Jan 27 '15 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.