Creating a Magento Script that Disables Coupons

As I mentioned in my previous post, I had to quickly find a way to disable a large number of coupons in Magento.

Generally I would say that it would be faster to do this by hand in the admin panel, but I had to disable 62 of them (and I was not sure that the solution would work so I did not want to put all that time into it only to not have it work).

Luckily this is a pretty easy task to accomplish. All you have to do is get a collection of shopping cart rules and change their is_active property to 0 (zero). Let’s take a look at this in the script below (I am omitting the “base script” info that was covered in this post).

$models = Mage::getModel('salesrule/rule')
	->getCollection()
	;

foreach($models as $model) {

	if($model->getRuleId() >= 154 && $model->getRuleId() getName() . "\n";
		$model->setIsActive(0);
		$model->save();
	}

	if($model->getRuleId() >= 196 && $model->getRuleId() getName() . "\n";
		$model->setIsActive(0);
		$model->save();
	}

}

Now I know there are better ways to get only the ID ranges of the coupons, but I was in a hurry and it works. Basically you can see that I have loaded a collection of salesrule/rule objects and am simply setting is_active to zero and saving the object.

Pretty simple eh?

Leave a Reply

Your email address will not be published. Required fields are marked *