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?

1 Answer 1

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

7
  • Code : paste.ofcode.org/pnHg7Eaup7WVdJLXNiqV5M outut : snag.gy/t8J41H.jpg @Sweety Masmiya
    – zus
    Commented Apr 16, 2019 at 7:39
  • I am using same code and I getting proper result. see the output :- prnt.sc/ncl4nv Commented Apr 16, 2019 at 8:40
  • In which location you are create your file ? Commented Apr 16, 2019 at 8:41
  • Actually this is my custom PHP page which is located in root folder. Path => public_html/code.php and my code execution like magento.com/code.php
    – zus
    Commented Apr 16, 2019 at 9:03
  • Yes. I am also doing same. Commented Apr 16, 2019 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.