Magento – Disable shipping method based on category

categorycheckoutshipping-methods

Currently I'm working on a new shipping method. I want this shipping method (pick-up at store) to be enabled for all categories, except for the category 'sale'. By category I mean the categories you can create in the backend of Magento, under the tab 'Products'. I did some research, but can't find a working code.

Best Answer

First of all,you need to track how a cart item is cart from Sales Category as magento system does not save the category details at checkout relate tables.

As per as magento system collectRates function of shipping method is responsible for a shipping will shown or not on Checkout.

There collectRates method you can get cart item details on by

if ($request->getAllItems()) {
                foreach ($request->getAllItems() as $item) {
            $product=$item->getProduct();
        }
    }

Solution: need to save category id at sales_flat_quote_item table during cart then on basic of this field disable shipment method.