0

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.

0

You can use

$country = Mage::getModel(’directory/country’)->load($country_id);
$countryName = $country->getName();
  • I am learning stage in PHP, How can i apply with my code? @Peter Horitzer – zus May 20 at 12:07
  • code with error : paste.ofcode.org/BFKxjt3VBFeSjqxQquYDZE error : Fatal error: Uncaught Error: Call to a member function load() on boolean in /home/abc/public_html/kolupadi_extension_order/sp_cost.php:14 Stack trace: #0 /home/abc/public_html/kolupadi_extension_order/sp_cost.php(42): getShippingEstimate('14419', '1', 'IN', '642001') #1 {main} thrown in /home/abc/public_html/kolupadi_extension_order/sp_cost.php on line 14 – zus May 20 at 12:15
  • In your case $country_id should be 'IN' Alternativ you could try $country = Mage::getModel('directory/country')->loadByCode($countryCode); – Peter Horitzer May 20 at 13:06

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.