Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Highlight popular countries in country select #4089

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions app/code/core/Mage/Checkout/Block/Onepage/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,12 @@ public function getAddressesHtmlSelect($type)
*/
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();
return Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect(
$this->getAddress()->getCountryId(),
$type . '[country_id]',
$type . ':country_id',
$this->helper('checkout')->__('Country'),
);
}

/**
Expand All @@ -196,6 +186,7 @@ public function getRegionHtmlSelect($type)
}

/**
* @deprecated
* @return bool|mixed
* @throws Mage_Core_Model_Store_Exception
*/
Expand Down Expand Up @@ -239,5 +230,4 @@ public function isShow()
{
return true;
}
/* */
}
28 changes: 27 additions & 1 deletion app/code/core/Mage/Directory/Block/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*
* @category Mage
* @package Mage_Directory
* @phpstan-type Option array{label: string, value: non-falsy-string}
*
* @method int getRegionId()
*/
Expand Down Expand Up @@ -75,7 +76,7 @@ public function getCountryHtmlSelect($defValue = null, $name = 'country_id', $id
->setTitle(Mage::helper('directory')->__($title))
->setClass('validate-select')
->setValue($defValue)
->setOptions($options)
->setOptions($this->sortCountryOptions($options))
->getHtml();

Varien_Profiler::stop('TEST: ' . __METHOD__);
Expand Down Expand Up @@ -169,4 +170,29 @@ public function getRegionsJs()
Varien_Profiler::stop('TEST: ' . __METHOD__);
return $regionsJs;
}

/**
* @template T of Option[]
* @param T $countryOptions
* @return array{0: array{label: string, value: Option[]}, 1: array{label: string, value: Option[]}}|T
*/
private function sortCountryOptions(array $countryOptions): array
{
$topCountryCodes = $this->helper('directory')->getTopCountryCodes();
$headOptions = $tailOptions = [];

foreach ($countryOptions as $countryOption) {
fballiano marked this conversation as resolved.
Show resolved Hide resolved
if (in_array($countryOption['value'], $topCountryCodes)) {
$headOptions[] = $countryOption;
} else {
$tailOptions[] = $countryOption;
}
}

if (empty($headOptions)) {
return $countryOptions;
}

return [['label' => $this->__('Popular'), 'value' => $headOptions], ['label' => $this->__('Others'), 'value' => $tailOptions]];
ma4nn marked this conversation as resolved.
Show resolved Hide resolved
}
}
12 changes: 12 additions & 0 deletions app/code/core/Mage/Directory/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,16 @@ public function isRegionRequired($countryId)
}
return in_array($countryId, $countyList);
}

/** @return list<string> */
public function getTopCountryCodes(): array
{
$topCountries = array_filter(explode(',', (string)Mage::getStoreConfig('general/country/top_countries')));

$transportObject = new Varien_Object();
$transportObject->setData('top_countries', $topCountries);
Mage::dispatchEvent('directory_get_top_countries', ['topCountries' => $transportObject]);

return $transportObject->getData('top_countries');
}
}
10 changes: 10 additions & 0 deletions app/code/core/Mage/Directory/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@
<show_in_store>0</show_in_store>
<can_be_empty>1</can_be_empty>
</optional_zip_countries>
<top_countries translate="label">
<label>Top Countries</label>
ma4nn marked this conversation as resolved.
Show resolved Hide resolved
<frontend_type>multiselect</frontend_type>
<sort_order>40</sort_order>
<source_model>adminhtml/system_config_source_country</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<can_be_empty>1</can_be_empty>
</top_countries>
</fields>
</country>
<region translate="label">
Expand Down
1 change: 1 addition & 0 deletions skin/frontend/base/default/js/opcheckout.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ Shipping.prototype = {
this.form = form;
if ($(this.form)) {
$(this.form).observe('submit', function(event){this.save();Event.stop(event);}.bind(this));
$(this.form).select('#shipping\\:country_id').first()?.addEventListener('change', () => { if (window.shipping) shipping.setSameAsBilling(false) });
}
this.addressUrl = addressUrl;
this.saveUrl = saveUrl;
Expand Down
Loading