donglin9068 2012-10-30 21:28
浏览 55
已采纳

以编程方式删除大量Magento优惠券(购物车价格规则)

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).

enter image description here

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条回答 默认 最新

  • duanke2503 2012-10-30 22:44
    关注

    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 />"; 
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号