I have a question on shipping. Is possible to set a method that takes the volume or weight of the product and calculate the cost depending on a scale?
Magento – Change shipping rate based on the weight or volume
productshippingshipping-methodsshipping-taxtax
Related Solutions
I found the solution from this link here : http://www.magentocommerce.com/boards/viewthread/316685/
In my observer I added below code to change my shipping rates as per country selection in shipping address.
class Pnk_Zones_Model_Observer
{
public function getShippingMethods($observer)
{
$session = Mage::getSingleton('checkout/session');
$quote=Mage::getSingleton('checkout/session')->getQuote();
$quoteid=$quote->getId();
if($quoteid) {
try{
$address=$quote->getShippingAddress();
if($address->getAddressType()=='shipping'){
// echo '<pre>'; print_r($events->getQuoteAddress()->getData()); exit;
//$price=40;
$countryId = $address->getCountry();
//Code to get Shipping rate from my zone module //
$shippingRateCollection = Mage::getModel('zones/zones')->getCollection();
$shippingRateCollection->addFieldToSelect('*');
$shippingRateCollection->getSelect()->where("find_in_set('".$countryId."', `countries`)");
foreach ($shippingRateCollection as $shipping){
$price = $shipping->getShippingRate();
}
// echo "Price = ".$price;
// Find if our shipping has been included.
$rates = $address->collectShippingRates()
->getGroupedAllShippingRates();
foreach ($rates as $carrier) {
foreach ($carrier as $rate) {
// Check with your custom shipping method code
if($rate->getCode() == 'zones'){
$rate->setPrice($price);
$rate->save();
}
}
}
$this->collectTotals($quote,$price);
}
$quote->collectTotals();
} catch (Exception $e) {
Mage::logException($e);
$response['error'] = $e->getMessage();
}
}
}
public function collectTotals($quote,$price){
$quoteid=$quote->getId();
$shippingcode='freeshipping_freeshipping';
if($quoteid) {
try{
$quote->setSubtotal(0);
$quote->setBaseSubtotal(0);
$quote->setSubtotalWithDiscount(0);
$quote->setBaseSubtotalWithDiscount(0);
$quote->setGrandTotal(0);
$quote->setBaseGrandTotal(0);
$quote->getShippingAddress()->setShippingMethod($shippingcode)/* ->collectTotals() */->save();
$quote->save();
foreach ($quote->getAllAddresses() as $address) {
$address->setSubtotal(0);
$address->setBaseSubtotal(0);
$address->setGrandTotal(0);
$address->setBaseGrandTotal(0);
$address->collectTotals();
$quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
$quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());
$quote->setSubtotalWithDiscount(
(float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
);
$quote->setBaseSubtotalWithDiscount(
(float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
);
$quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
$quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());
$address->setShippingAmount($price);
$address->setBaseShippingAmount($price);
$address->save();
}
$response['message'] = 'Succcess';
} catch (Exception $e) {
Mage::logException($e);
$response['error'] = $e->getMessage();
}
}
}
}
Paul,Magento have magic logic to change the weight of cart item
.When magento is added a product in cart it fire an event sales_quote_item_set_product
and it send two params
one is Quote item(Quote Object)
and another is Current Carted product object(Mage_Catalog_Model_Product
)
See at Class Mage_Sales_Model_Quote_Item
event
Mage::dispatchEvent('sales_quote_item_set_product', array(
'product' => $product,
'quote_item' => $this
));
I suggest to the trigger an event/observer on this occasion and you can change weight of quote items using setter
function setWeight()
and another thing is that magento is save quote item weight
at sales_flat_quote_item
Table at database.
Code of Observer:
<?php
class Stackexchange_Magento53367_Model_Observer
{
public function setweightCustomOption(Varien_Event_Observer $observer)
{
$Event = $observer->getEvent();
//Mage::dispatchEvent('sales_quote_item_set_product', array('product' => $product,'quote_item' => $this));
$product=$Event->getProduct();
$quote_item=$Event->getQuoteItem();
$quote_item->setWeight(900);
}
}
Best Answer
Try Table Rates shipping method. Their you have an option as per your need.
If you want to do some custom calculation you can make use of the
checkout_cart_save_before
event.