3

PHP Shipping Estimation script (From StackExchange )

    <?php
//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');

// require_once 'app/Mage.php'; 
// Mage::app('main');

//Mage::app();

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

    $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->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('14419','1',"IN","642001"));
echo "</pre>";

?>

Error :

Fatal error:  Uncaught Mage_Core_Model_Store_Exception in /home/abc/public_html/app/code/core/Mage/Core/Model/App.php:1377
Stack trace:
#0 /home/abc/public_html/app/code/core/Mage/Core/Model/App.php(861): Mage_Core_Model_App->throwStoreException()
#1 /home/abc/public_html/sp_cost.php(17): Mage_Core_Model_App->getStore('default')
#2 /home/abc/public_html/sp_cost.php(43): getShippingEstimate('14419', '1', 'IN', '642001')
#3 {main}
  thrown in /home/abc/public_html/app/code/core/Mage/Core/Model/App.php on line 1377

How to solve the error and get estimation shipping charge?

0
<?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());

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.