0

I followed this tutorial to add some new shipping methods to my website

http://www.smashingmagazine.com/2014/01/15/create-custom-shipping-methods-magento/#more-130404

However i now need to disable these methods if an If statement is true. I already have the if statement which unsets the free shipping so i know that works. however i do seem to be able to unset the custom shipping methods. I know it is a long shot but does anyone know how i would go about this??

This is the IF statement i am using to unset free shipping if that helps

<?php if (($_shippingRateGroups = $this->getEstimateRates())): ?>
<form id="co-shipping-method-form" action="<?php echo $this->getUrl('checkout/cart/estimateUpdatePost') ?>">
                               <?php $quote = Mage::getSingleton('checkout/session')->getQuote();
                        $cartItems = $quote->getAllVisibleItems();
                        foreach ($cartItems as $item)
                    {
                        $productId = $item->getProductId();
                        $product = Mage::getModel('catalog/product')->load($productId);
                               if($product->getData('direct_delivery_product')==1){?>

                         <div class="direct_message">
                         <?php echo 'You have direct delivery products in your shopping cart. This means it may take longer than the standard 2-5 working days to deliver.';?></div>

                            <?php unset($_shippingRateGroups["freeshipping"]);
                             break;
                         }

                    }

                ?>

New collectRate method

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    //return nothing if the shipping method is disabled from config
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    $productId = $item->getProductId();
                        $product = Mage::getModel('catalog/product')->load($productId);
                               if($product->getData('direct_delivery_product')==0){

         return false;
    }

    $result = Mage::getModel('shipping/rate_result');
        /* @var $result Mage_Shipping_Model_Rate_Result */

        $result->append($this->_getStandardShippingRate());
        $result->append($this->_getExpressShippingRate());

        return $result;
}

Thank you if you can help.

1

I assume you have a model associated to the shipping method.
In that module you must have a method called collectRates() that should return the costs of the available methods.
Just make your method look like this:

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    //return nothing if the shipping method is disabled from config
    if (!$this->getConfigFlag('active')) {
        return false;
    }
    //insert your logic here that determines if the method should be disabled
    //...and then
    if (your disable condition is met) {
         return false;
    }
    //rest of the code goes here
}

[EDIT]
The code you added does not work because the variable $item is not defined when you use it.
From your code I assume you want to not show the delivery method if at least one product in the cart has the value 0 for the attribute direct_delivery_product.
In case I'm right try this:

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    //return nothing if the shipping method is disabled from config
    if (!$this->getConfigFlag('active')) {
        return false;
    }
    $items = $request->getAllItems();
    foreach ($items as $item) {
        $productId = $item->getProductId();
        $product = Mage::getModel('catalog/product')->load($productId);
        if(!$product->getData('direct_delivery_product')){
            return false;
        }
    }
    //rest of your code here
}

Even if loading products in a loop is not the best practice, I think you should worry about optimizing later. This should make it work.

  • hey i added your code, but it didnt work. I think it was just my coding rather than your fix as my knowledge isnt to great. Could you have a look at the added code to my question and advise please. Thank you – Adam Allen Sep 3 '14 at 9:38
  • @AdamAllen. See my update – Marius Sep 3 '14 at 9:44
  • you sir are a legend!! thank you it works perfectly!! – Adam Allen Sep 3 '14 at 10:11
  • i had to change if(!$product->getData('direct_delivery_product')) to if($product->getData('direct_delivery_product')) Other than that works perfect thank you – Adam Allen Sep 3 '14 at 10:30

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.