3

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

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);
}
0

Made a Observer and add below function

public function add_default_shipping ($observer)
{  
    $country = Mage::getStoreConfig('shipping/origin/country_id');
    $state = Mage::getStoreConfig('shipping/origin/region_id');
    $postcode = Mage::getStoreConfig('shipping/origin/postcode');

    Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()
                ->setCountryId($country)
                ->setRegionId($state)
                ->setPostcode($postcode)
                ->setCollectShippingRates(true);
              $quote->save();



    $code       = 'freeshipping_freeshipping';
    $grandTotal = (int) Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
    if ($grandTotal < MST_Switchshipping_Model_Observer::COMPARE_PRICE) {
        $code='matrixrate_matrixrate_11';
    }
    Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->setShippingMethod($code);
    return $this;
}

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.