Custom Carrier; price depends on grandtotal of quote
-
14-12-2020 - |
Question
I made a custom module for my own carrier which has multiple options where the shippingcosts depend on the order total. Really simple; is it more or less than 50 euros?
MyModule/CustomCarrier/Model/Carrier/Method/Evening.php
class MyModule_CustomCarrier_Model_Carrier_Method_Evening extends
MyModule_CustomCarrier_Model_Carrier_Method_Abstract
{
public function getCost() {
return 0;
}
public function getPrice()
{
$totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals();
$subtotal = $totals["subtotal"]->getValue();
$grandtotal = $totals["grand_total"]->getValue();
if ($grandtotal < 50) {
return 10;
} else {
return 5;
}
}
}
So simply put whenever the quote Totals are less than 50 euros, the shipping is 10. If more, the shipping is 5.
When I place an order higher than 50 euros, I see my carrier show up with a price of 5,- in the checkout, however on the review block with the totals it shows up as 10.
When I log the values, I see while going to checkout that the variables get logged a lot at once, and sometimes the $grandtotal
var is 0. Why is that? How can I make sure it'll get the right amount all of the time?
Solution
Figured it out. In stead of getting the totals from checkout/session
, I took the order by ID and got the total from the order rather then the session. This way it's always the same during the entire checkout.
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
$grandtotal = $order->getGrandTotal();
edit: This didn't seem to work out properly as well, so now this seems to work
$quote = Mage::getModel('checkout/session')->getQuote();
$quoteData = $quote->getData();
$grandtotal = $quoteData['grand_total'];