0

Just need as I asked in the main question title.

Right now I have created the below function in my observer which executes on event sales_quote_collect_totals_before, with below code:

public function validateFreeShipping(Varien_Event_Observer $observer) {
    //$quote = $observer->getEvent()->getQuote();
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $quote->getShippingAddress()->setFreeShipping($setFreeShipping);
    $quoteItems = $quote->getAllItems();
    $setFreeShipping = true;
    foreach ($quoteItems as $quoteItem) {
        $itemSku = $quoteItem->getSku();
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $itemSku);
        $prodCatIds = $product->getCategoryIds();
    }
    # echo "<pre/>";print_r($prodCatIds);
    foreach ($prodCatIds as $catId) {
        $category = Mage::getModel('catalog/category')->load($catId);
        $allowFreeshipping = $category->getAllowFreeshipping();
        if($allowFreeshipping == "194") $setFreeShipping = false;
    }
    $address = $quote->getShippingAddress();
    $quote->getShippingAddress()->setFreeShipping($setFreeShipping);
    $quote->collectTotals()->save();
}

But this never seems to work, and I always get "Free Shipping" method enabled no matter what.

Can anyone suggest a standard way to do this which involves event catching observers ?

| improve this question | | | | |
0

There is one event payment_method_is_active for payment method filtration. We don’t have similar event: shipping_method_is_active available for shipping method.

But You can filter by overriding Mage_Shipping_Model_Shipping::collectCarrierRates().

Step 1: Create a file Mageweb_ShippingFilter.xml in app > etc > modules and paste below code in this file.

<?xml version="1.0"?>
   <config>
      <modules>
         <Mageweb_ShippingFilter>
            <active>true</active>
            <codePool>community</codePool>
            <version>0.1.0</version>
         </Mageweb_ShippingFilter>
      </modules>
   </config>

Step 2: Create Data.php file in app > code > community > Mageweb > ShippingFilter > Helper and paste below code.

<?php
class Mageweb_ShippingFilter_Helper_Data extends Mage_Core_Helper_Abstract
{
}

Step 3: Create Shipping.php file in app > code > community > Mageweb > ShippingFilter > Model > Shipping and paste below code.

<?php 
class Mageweb_ShippingFilter_Model_Shipping_Shipping extends Mage_Shipping_Model_Shipping
{
// here you can add code
public function collectCarrierRates($carrierCode, $request)
{
    if (!$this->_checkCarrierAvailability($carrierCode, $request)) {
        return $this;
    }
    return parent::collectCarrierRates($carrierCode, $request);
}

protected function _checkCarrierAvailability($carrierCode, $request = null)
{
    $isLoggedIn  = Mage::getSingleton('customer/session')->isLoggedIn();
    if(!$isLoggedIn){
        if($carrierCode == 'flatrate'){ #Hide Flat Rate for non logged in customers
            return false;
        }
    }
    return true;
}
}

Step 4: Step 3: Create config.xml file in app > code > community > Mageweb > ShippingFilter > etc and paste below code.

<?xml version="1.0"?>
<config>
   <modules>
      <Mageweb_ShippingFilter>
         <version>0.1.0</version>
      </Mageweb_ShippingFilter>
    </modules>
    <global>
       <helpers>
          <shippingfilter>
             <class>Mageweb_ShippingFilter_Helper</class>
          </shippingfilter>
       </helpers>
       <models>
          <shippingfilter>
             <class>Mageweb_ShippingFilter_Model</class>
             <resourceModel>shippingfilter_mysql4</resourceModel>
          </shippingfilter>
          <shipping>
            <rewrite>
                <shipping>Mageweb_ShippingFilter_Model_Shipping_Shipping</shipping>
            </rewrite>
         </shipping>
      </models>
   </global>
</config> 

You can add/edit code in Mageweb_ShippingFilter_Model_Shipping_Shipping file.

| improve this answer | | | | |

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.