Last active
August 29, 2015 14:06
-
-
Save PhilKershaw/16286855c3f5259ccc07 to your computer and use it in GitHub Desktop.
Simple tool for deleting Sales Rules in Magento
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple tool for deleting Sales Rules | |
* | |
* @TODO add function to delete by ID | |
* | |
* @author Phil Kershaw | |
*/ | |
require_once '../app/Mage.php'; | |
class deleteSalesRules | |
{ | |
/** | |
* The rules collection | |
* @var array | |
*/ | |
private $rules; | |
public function __construct() | |
{ | |
// Start app | |
Mage::app(); | |
// Fetch all rules | |
$this->rules = Mage::getResourceModel('salesrule/rule_collection')->load(); | |
} | |
/** | |
* Delete all rules which match the string given. Will also delete partial matches unless $strict is true. | |
* @param $name string | |
* @param $strict bool | |
*/ | |
public function deleteByName($name, $strict = false) | |
{ | |
foreach ($this->rules as $code) { | |
if ($strict) { | |
if ($name == $code->getName()) { | |
echo "Deleting {$code->getName()}\n"; | |
$code->delete(); | |
} | |
} else { | |
if (preg_match('/'.$name.'/i', $code->getName())) { | |
echo "Deleting {$code->getName()}\n"; | |
$code->delete(); | |
} | |
} | |
} | |
} | |
} | |
$rules = new deleteSalesRules(); | |
$rules->deleteByName('alert #'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment