cancel
Showing results for 
Search instead for 
Did you mean: 

How to use with Country Name instead of Country Code?

SOLVED

How to use with Country Name instead of Country Code?

Here is my code about shipping Estimation cost script,

 

<?php
ob_start();
require_once('./../app/Mage.php');
umask(0);
ini_set('display_errors',true);
ini_set('memory_limit', '1024M');
Mage::app()->loadArea('frontend');

function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {

    $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
$_product = Mage::getModel('catalog/product')->load($productId);

$_product->getStockItem()->setUseConfigManageStock(false);
$_product->getStockItem()->setManageStock(false);

$quote->addProduct($_product, $productQty);
$quote->getShippingAddress()->setcountryId($countryId)->setPostcode($postcode);
$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();

$_rates = $quote->getShippingAddress()->getShippingRatesCollection();

$shippingRates = array();
foreach ($_rates as $_rate):
if($_rate->getPrice() > 0) {
$shippingRates[] = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
}
endforeach;

return $shippingRates;

}

$results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value

$count = -1;

foreach ($results as $result):
$count++;
?>
<option value="<?php echo $result["Price"]; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"];?>
</option>

<?php
endforeach;

?>

 

How to get shipping cost based on country name instead of country code?

 

FYI - $results = getShippingEstimate('14419','1',"IN","642001"); //IN should be INDIA.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to use with Country Name instead of Country Code?

I have updated the code.
Please try once.

<?php
ob_start();
require_once('app/Mage.php');
umask(0);
ini_set('display_errors',true);
ini_set('memory_limit', '1024M');
Mage::app()->loadArea('frontend');


function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {

    $countryName = ucwords(strtolower($countryId));
    $countries = Mage::app()->getLocale()->getCountryTranslationList();;
    $countryId = array_search($countryName, $countries);


// Select website
    $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
    $_product = Mage::getModel('catalog/product')->load($productId);

    $_product->getStockItem()->setUseConfigManageStock(false);
    $_product->getStockItem()->setManageStock(false);

    $quote->addProduct($_product, $productQty);
    $quote->getShippingAddress()->setcountryId($countryId)->setPostcode($postcode);
    $quote->getShippingAddress()->collectTotals();
    $quote->getShippingAddress()->setCollectShippingRates(true);
    $quote->getShippingAddress()->collectShippingRates();

    $_rates = $quote->getShippingAddress()->getShippingRatesCollection();

    $shippingRates = array();
    foreach ($_rates as $_rate):
        if($_rate->getPrice() > 0) {
            $shippingRates[] = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
        }
    endforeach;

    return $shippingRates;

}



$results = getShippingEstimate('14419','1',"INDIA","642001"); // Predefined Value

$count = -1;

// echo "<select id='shipping_cost'>";


echo "<option>---Select---</option>";
foreach ($results as $result):
    $count++;
    ?>
    <option value="<?php echo $result["Price"]; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"];?>
    </option>

<?php
endforeach;

// echo "</select>";

?>

View solution in original post

10 REPLIES 10

Re: How to use with Country Name instead of Country Code?

Hi @Aveeva 
You can get country name using country code.
Insert following code:

If you have the country code and you want to get the country name use the below code:

$country_name=Mage::app()->getLocale()->getCountryTranslation($country_code);

Or you can also try

// $countryCode looks like "US"
$country = Mage::getModel('directory/country')->loadByCode($countryCode);
echo $country->getName(); /

I hope it will help you!

Re: How to use with Country Name instead of Country Code?

@Vimal Kumar    I am learning stage in PHP how can i apply in my code. 

 

My Code :

 

<?php
ob_start();
require_once('./../app/Mage.php');
umask(0);
ini_set('display_errors',true); 
ini_set('memory_limit', '1024M');
Mage::app()->loadArea('frontend');



function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {
   
    // Select website
	$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
    $_product = Mage::getModel('catalog/product')->load($productId);

    $_product->getStockItem()->setUseConfigManageStock(false);
    $_product->getStockItem()->setManageStock(false);

    $quote->addProduct($_product, $productQty);
    $quote->getShippingAddress()->setcountryId($countryId)->setPostcode($postcode); 
    $quote->getShippingAddress()->collectTotals();
    $quote->getShippingAddress()->setCollectShippingRates(true);
    $quote->getShippingAddress()->collectShippingRates();

    $_rates = $quote->getShippingAddress()->getShippingRatesCollection();

    $shippingRates = array();
    foreach ($_rates as $_rate):
            if($_rate->getPrice() > 0) {
                $shippingRates[] =  array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
            }
    endforeach;

    return $shippingRates;

}

$results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value
echo "<option>---Select---</option>";
foreach ($results as $result): 
$count++;
?>
 <option value="<?php echo $result["Price"]; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"];?> 
 </option>

 <?php
endforeach;

// echo "</select>"; 

?> 

 

In my function $countryId

 

Re: How to use with Country Name instead of Country Code?

Hello @Aveeva 

 

you can use below code to get code from contry name

$countryName = 'your country name here';
$countries = Mage::app()->getLocale()->getCountryTranslationList();;
$countryCode = array_search($countryName, $countries);

if works then mark as a solution Smiley Happy 

Keep doing help to others Smiley Happy


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How to use with Country Name instead of Country Code?

Hi @Aveeva 
Try once below code:

function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {

    $countryModel = Mage::getModel('directory/country')->loadByName($countryId);
    $countryId = $countryModel->getCountryCode();

    // Select website
	$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
    $_product = Mage::getModel('catalog/product')->load($productId);

    $_product->getStockItem()->setUseConfigManageStock(false);
    $_product->getStockItem()->setManageStock(false);

    $quote->addProduct($_product, $productQty);
    $quote->getShippingAddress()->setcountryId($countryId)->setPostcode($postcode);
    $quote->getShippingAddress()->collectTotals();
    $quote->getShippingAddress()->setCollectShippingRates(true);
    $quote->getShippingAddress()->collectShippingRates();

    $_rates = $quote->getShippingAddress()->getShippingRatesCollection();

    $shippingRates = array();
    foreach ($_rates as $_rate):
            if($_rate->getPrice() > 0) {
                $shippingRates[] =  array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
            }
    endforeach;

    return $shippingRates;

}

$results = getShippingEstimate('14419','1',"IN","642001"); // Predefined Value
echo "<option>---Select---</option>";
foreach ($results as $result):
$count++;
?>
 <option value="<?php echo $result["Price"]; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"];?>
 </option>

 <?php
endforeach;

// echo "</select>";

?>


$results = getShippingEstimate('14419','1',"India","642001"); //IN should be INDIA

Re: How to use with Country Name instead of Country Code?

Error:

 

Fatal error: Uncaught Varien_Exception: Invalid method Mage_Directory_Model_Country::loadByName(Array ( [0] => INDIA ) ) in /home/abc/public_html/lib/Varien/Object.php:653 Stack trace: #0 /home/abc/public_html/kolupadi_extension_order/sp_cost.php(13): Varien_Object->__call('loadByName', Array) #1 /home/abc/public_html/kolupadi_extension_order/sp_cost.php(51): getShippingEstimate('14419', '1', 'INDIA', '642001') #2 {main} thrown in /home/abc/public_html/lib/Varien/Object.php on line 653

Re: How to use with Country Name instead of Country Code?

Final Cde :

 

<?php
ob_start();
require_once('./../app/Mage.php');
umask(0);
ini_set('display_errors',true);
ini_set('memory_limit', '1024M');
Mage::app()->loadArea('frontend');



function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {

$countryModel = Mage::getModel('directory/country')->loadByName($countryId);
$countryId = $countryModel->getCountryCode();

// Select website
    $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
$_product = Mage::getModel('catalog/product')->load($productId);

$_product->getStockItem()->setUseConfigManageStock(false);
$_product->getStockItem()->setManageStock(false);

$quote->addProduct($_product, $productQty);
$quote->getShippingAddress()->setcountryId($countryId)->setPostcode($postcode);
$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();

$_rates = $quote->getShippingAddress()->getShippingRatesCollection();

$shippingRates = array();
foreach ($_rates as $_rate):
if($_rate->getPrice() > 0) {
$shippingRates[] = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
}
endforeach;

return $shippingRates;

}



$results = getShippingEstimate('14419','1',"INDIA","642001"); // Predefined Value

$count = -1;

// echo "<select id='shipping_cost'>";


echo "<option>---Select---</option>";
foreach ($results as $result):
$count++;
?>
<option value="<?php echo $result["Price"]; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"];?>
</option>

<?php
endforeach;

// echo "</select>";

?>

Re: How to use with Country Name instead of Country Code?

I have updated the code.
Please try once.

<?php
ob_start();
require_once('app/Mage.php');
umask(0);
ini_set('display_errors',true);
ini_set('memory_limit', '1024M');
Mage::app()->loadArea('frontend');


function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {

    $countryName = ucwords(strtolower($countryId));
    $countries = Mage::app()->getLocale()->getCountryTranslationList();;
    $countryId = array_search($countryName, $countries);


// Select website
    $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('english')->getId());
    $_product = Mage::getModel('catalog/product')->load($productId);

    $_product->getStockItem()->setUseConfigManageStock(false);
    $_product->getStockItem()->setManageStock(false);

    $quote->addProduct($_product, $productQty);
    $quote->getShippingAddress()->setcountryId($countryId)->setPostcode($postcode);
    $quote->getShippingAddress()->collectTotals();
    $quote->getShippingAddress()->setCollectShippingRates(true);
    $quote->getShippingAddress()->collectShippingRates();

    $_rates = $quote->getShippingAddress()->getShippingRatesCollection();

    $shippingRates = array();
    foreach ($_rates as $_rate):
        if($_rate->getPrice() > 0) {
            $shippingRates[] = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
        }
    endforeach;

    return $shippingRates;

}



$results = getShippingEstimate('14419','1',"INDIA","642001"); // Predefined Value

$count = -1;

// echo "<select id='shipping_cost'>";


echo "<option>---Select---</option>";
foreach ($results as $result):
    $count++;
    ?>
    <option value="<?php echo $result["Price"]; ?>"> <?php echo $result["Title"]." - Rs ".$result["Price"];?>
    </option>

<?php
endforeach;

// echo "</select>";

?>

Re: How to use with Country Name instead of Country Code?

Working, thanks for your help

Re: How to use with Country Name instead of Country Code?

Can you pls explain.