Magento – Set origin address for shipping rate estimation

shipping

I have the following script that echoes a dollar value for UPS ground shipping for the given product given the address of the school it will be shipped to in the database. I want to be able to also change the origin region, state, and postal code for the rate estimation. There are multiple rate destinations so I can't just do it in the back end, I need to be able to do it programmatically. How do I do that?

<?php
$dbname='secret';
require_once('connect.php');
require_once('app/Mage.php');
umask(0);
Mage::app(); 

$product_id = $_POST['productID'];
$customer_id = Mage::getSingleton('customer/session')->getCustomerId();
$productQty = 1000;

$customer = Mage::getModel('customer/customer')->load($customer_id);
$customerSchool = $customer->getSchool();

/* Get info about school */
$sql = 'SELECT  mail_zip FROM schools WHERE school_name =  "' . $customerSchool . '"';

foreach ($con->query($sql) as $row) {
    $mail_zip= $row['mail_zip'];
}

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

    $_product = Mage::getModel('catalog/product')->load($product_id);

    $_product->getStockItem()->setUseConfigManageStock(false);
    $_product->getStockItem()->setManageStock(false);
    $quote->addProduct($_product, $productQty);
    $quote->getShippingAddress()->setCountryId('US')->setPostcode($mail_zip); 
    $quote->getShippingAddress()->collectTotals();
    $quote->getShippingAddress()->setCollectShippingRates(true);
    $quote->getShippingAddress()->collectShippingRates();

    $_rates = $quote->getShippingAddress()->getShippingRatesCollection();

    foreach ($_rates as $_rate){
        if($_rate->getCode()=="ups_GND")
            echo($_rate ->getPrice());
    }
?>

Best Answer

You can do this with setConfig, and it will persist through the current session. If you do not call save, it will not save to the db.

Example:

$store = Mage::app()->getStore();
$configScope = array(
    'street_line1' => $street1,
    'street_line2' => $street2,
    'city'         => $city,
    'region_id'    => $region_id,
    'postcode'     => $postcode,
    'country_id'   => $country_id
    );

foreach($configScope as $_scope=>$_val){
    $store->setConfig('shipping/origin/' . $_scope, $_val);
}
Related Topic