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.