0

I want to add onchange function in select of country in cart page. But country are coming from :

Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId())

So , How to add onchange function to country on Cart page.

When customer select country then page will reload and display delivery method in dropdown and calculate estimate shipping cost and add in grand total in cart page.

enter image description here

  • there will be form for estimate shipping calculation in frontend/rwd/default/template/checkout/cart/shipping.phtml (default rwd) or find in you theme – Gopal Patel May 15 '17 at 12:29
  • Yes , in that form, Country is coming dynamic from this code : Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()), So how to edit this ? – BornCoder May 15 '17 at 13:02
  • there will be two form but there is no submit button in your attached screenshot – Gopal Patel May 15 '17 at 13:21
  • I want to remove submit and i want to apply submit action on change of country. Is it possible ? – BornCoder May 15 '17 at 13:24
0

This is the function you are referring. At the end, select html is built by core/html_select block

public function getCountryHtmlSelect($defValue=null, $name='country_id', $id='country', $title='Country')
{
    Varien_Profiler::start('TEST: '.__METHOD__);
    if (is_null($defValue)) {
        $defValue = $this->getCountryId();
    }
    $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_'.Mage::app()->getStore()->getCode();
    if (Mage::app()->useCache('config') && $cache = Mage::app()->loadCache($cacheKey)) {
        $options = unserialize($cache);
    } else {
        $options = $this->getCountryCollection()->toOptionArray();
        if (Mage::app()->useCache('config')) {
            Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
        }
    }
    $html = $this->getLayout()->createBlock('core/html_select')
        ->setName($name)
        ->setId($id)
        ->setTitle(Mage::helper('directory')->__($title))
        ->setClass('validate-select')
        ->setValue($defValue)
        ->setOptions($options)
        ->getHtml();

    Varien_Profiler::stop('TEST: '.__METHOD__);
    return $html;
}

The good news for you are core/html_select block allows including some extra data, such as you want, using setExtraParams() method

For instance, take a look at Mage_Checkout_Block_Onepage_Abstract::getCountryHtmlSelect() method

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')
        ->setValue($countryId)
        ->setOptions($this->getCountryOptions());
    if ($type === 'shipping') {
        $select->setExtraParams('onchange="if(window.shipping)shipping.setSameAsBilling(false);"');
    }

    return $select->getHtml();
}

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.