I have a magento script that runs from command line that needs to get a price quote based off of the weight of an item. (Feel free to improve any of this script) I don't want to adjust the weight in the database but I want to adjust it for when I get the shipping quote.
<?php
setlocale(LC_MONETARY, 'en_US');
require_once("/usr/share/nginx/html/app/Mage.php");
umask(0);
ini_set('display_errors',true); Mage::setIsDeveloperMode(true);
Mage::app('admin')->setUseSessionInUrl(false);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$aprox='';
//Mage::app();
$mask = " %7s | %-30s | %-15s | %-30s \n";
// Need to figure out how to temporarily adjust a products weight without saving it.
function calculate_weight($weight){
// If under 1lb 1.5 x $weight
// If 1lb to 5lbs 1.25 x $weight
// If 5lb+ 1.2 x $weight
if ($weight < 1) { return $weight * 1.5;}
elseif ($weight < 5) { return $weight * 1.25;}
else return $weight * 1.2;
}
function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {
global $mask, $aprox;
$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId());
//Check if is SKU or Number.
if (is_numeric($productId)){
$_product = Mage::getModel('catalog/product')->load($productId);
} else
{
$model = Mage::getModel('catalog/product');
$product_id = $model->getIdBySku($productId);
$_product = $model->load($product_id);
//var_dump($prod);
//exit;
}
$cost = $_product->getCost();
$name = $_product->getName();
$minPriceWithShipping = $_product->getMinPriceWithShipping();
$shippingPrice = $_product->getShippingPrice();
$aprox = $shippingPrice+($cost*$productQty);
printf($mask, number_format((float)$cost, 2, '.',''), $name, number_format((float)$minPriceWithShipping, 2, '.',''),number_format((float)$aprox, 2, '.','') . ' QTY:'.$productQty);
$_product->getStockItem()->setUseConfigManageStock(false);
$_product->getStockItem()->setManageStock(false);
$weight = $_product->getWeight();
if ($weight <= 0) { echo $weight; return 9999.99; }
// php getShipping.php ons6467 1 57.87 1.6000 Weight
echo $weight." Weight \n";
$weight = calculate_weight($weight);
echo $weight." New Weight \n";
//echo $_product->setWeight($weight); <--- This breaks th code I just want to temporarily adjust the weight for calculations
// Don't want to save Weight
$quote->addProduct($_product, $productQty);
$quote->getShippingAddress()->setCountryId($countryId)->setPostcode($postcode)->setRegion('Main');
$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(), "Carrier" => $_rate->getCarrier(), "Carrier Title" => $_rate->getCarrierTitle());
}
endforeach;
return $shippingRates;
}
// Place this at the very top of script
$start = microtime(TRUE);
// product id, quantity, country, postcode
$c = (isset($argv[2])) ? $argv[2] : 1;
$rates = getShippingEstimate($argv[1] , $c , "US","04652");// Farthest continental USA Zip code from warehouse that I could find.
function sortr($a, $b) {
return $a['Price'] - $b["Price"];
}
usort($rates, "sortr");
//$fst = array_shift($rates);
// only min-width of cells is set
foreach($rates as $rate){
printf($mask, number_format((float)$rate['Price'], 2, '.','') , $rate['Title'],strtoupper($rate['Carrier']) ,'');
}
if (isset($argv[3])) {
$v= $argv[3]-$aprox;
echo "$".number_format((float)$v, 2, '.','') . " Estimated Profit. \n";
}
$finish = microtime(TRUE);
// Subtract the start time from the end time to get our difference in seconds
$totaltime = $finish - $start;
echo "This script took ".$totaltime." seconds to run \n";
$_product->setWeight($weight);
?