Skip to main content
3 of 3
added 1591 characters in body
Sweety Masmiya
  • 1.4k
  • 11
  • 21
<?php 
    ini_set('display_startup_errors', 1); 
    error_reporting(E_ALL);
//require_once("Mage.php"); 
    ob_start(); 
    require_once(__DIR__ . '/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 ) { 

 // Change below line based on your store_view_code.
    $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('{store_view_code}')->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; 
    } 
    echo "<pre>"; 
    //product id, quantity, country, postcode 
    print_r(getShippingEstimate('13541','5',"IN","642001")); 
    echo "</pre>"; 
?>

If you use multiple store view than change below line based on your store_view_code.

$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('{store_view_code}')->getId());

Sweety Masmiya
  • 1.4k
  • 11
  • 21