Deleting a large number of Magento coupons (shopping cart price rules) programatically

风格不统一 提交于 2019-12-07 04:54:27

问题


I've been on here before asking about writing code to get Magento to generate a random coupon code for a new e-mail subscriber - Creating a single random Magento coupon

This code worked perfectly and to those who helped I am eternally grateful. I am now looking for a way to delete these coupons as they expire in a bulk fashion because deleting them individually through the Magento admin is a huge pain. As you can see from the picture below, in the span of a couple of days we generated over 300 coupons (all now expired).

Would modifying the create coupon code detailed in the link (from my original question) above work? Something like:

$model = Mage::getModel('salesrule/rule')
       $model->getName('New newsletter subscriber discount');
       $model->getToDate(date('Y-m-d'));
$model->delete();

Or am I totally barking up the wrong tree here?


回答1:


Sorry to answer my own question but almost as soon as I posted, I was able to figure it out. So in case anyone else has a question like mine:

ini_set('auto_detect_line_endings', true);

// Requires Mage
require_once('../../app/Mage.php');

//Initializes Mage
Mage::app('default');
deleteCoupon();

function deleteCoupon() {
  $collection = Mage::getModel('salesrule/rule')->getCollection()->load();

  foreach($collection as $model) {

    // Delete all new newsletter sub coupons
    if ($model->getName() == 'New newsletter subscriber discount') {

    // Delete all coupons expiring today    
    if ($model->getToDate() == date('Y-m-d')) { 
      $model->delete();
      echo "Deleted <br />";
    } else {
      echo "No coupons found! <br />"; 
    }
  }
}


来源:https://stackoverflow.com/questions/13147835/deleting-a-large-number-of-magento-coupons-shopping-cart-price-rules-programat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!