Here is my code about shipping Estimation cost script,
How to get shipping cost based on country name instead of country code?
FYI - $results = getShippingEstimate('14419','1',"IN","642001"); //IN should be INDIA.
Solved! Go to Solution.
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>";
?>
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!
@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
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
Keep doing help to others ![]()
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
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
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>";
?>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>";
?>
Working, thanks for your help
Can you pls explain.