1

I want to change shipping method depending on products present in the cart.
There are different weights assigned to products.
If all products present in the cart are of less than 5 k.g (weight for each product) then one shipping method will display.

If any one product in the cart, has weight greater than 5 kg. then show another shipping method.

1 Answer 1

0

You need to rewrite the collectRates method from the 2 methods you need and add this.
for the one that is allowed only if all the products are below 5kg:

$items = $request->getAllItems();
foreach ($items as $item) {
    if ($item->getProduct()->getWeight() > 5) {
         return false;
    }
}

and for the one that is allowed only if there is a product with weight over 5:

$items = $request->getAllItems();
$allowed = false;
foreach ($items as $item) {
    if ($item->getProduct()->getWeight() > 5) {
         $allowed = true;
         break;
    }
}
if (!$allowed) {
     return false;
}
7
  • Thank you for your answer.. But I am planning to do it via observer.. Is it right to do via observer? If yes which observer should I use? so that it will enable disable method on checkout.
    – vedu
    Oct 21, 2015 at 12:15
  • I don't know any event for the shipping methods...I'll post one if I find it.
    – Marius
    Oct 21, 2015 at 12:23
  • can you please tell me can I use your logic for Table Rates shipping method? Then I will override Flatrate.php and Tablerate.php.
    – vedu
    Oct 21, 2015 at 12:31
  • override Tablerate.php
    – Marius
    Oct 21, 2015 at 12:39
  • Really thank you for your guide.. now can you suggest some links for override Mage_Shipping_Model_Carrier_Tablerate. So that I can add new code. Sorry but I am a newbie in magento.
    – vedu
    Oct 21, 2015 at 12:42

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.