You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何删除购物车价格规则中的过期优惠券代码(表salesrule_coupon)

How to Delete Expired Coupon Codes (Not the Entire Sales Rule) in Magento

Got it, let's fix this so you can clean up expired coupons tied to your specific sales rule without deleting the rule itself. The original script you have is targeting sales rules instead of individual coupons—here's how to adjust it:

Corrected Script to Delete Expired Coupons

<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';

// Initialize Magento with admin permissions (required for deleting coupons)
Mage::app('admin');

// Replace this with your target sales rule ID (same as $rule_id in your coupon generator script)
$targetRuleId = YOUR_RULE_ID_HERE;

// Get today's date in Y-m-d format for expiry comparison
$today = date('Y-m-d');

// Load coupons linked to your target rule, filtered to only expired ones
$couponCollection = Mage::getModel('salesrule/coupon')
    ->getCollection()
    ->addFieldToFilter('rule_id', $targetRuleId)
    // Keep only coupons where expiry date is before today
    ->addFieldToFilter('to_date', ['lt' => $today])
    // Optional: Add prefix filter to target only your generated coupons (matches your original "XX" check)
    ->addFieldToFilter('code', ['like' => 'XX%']);

// Optional dry run: Uncomment to preview which coupons will be deleted without making changes
// echo "Expired coupons to delete: " . implode(', ', $couponCollection->getColumnValues('code')) . "\n";

// Iterate and delete each expired coupon
foreach ($couponCollection as $coupon) {
    try {
        $coupon->delete();
        echo "Successfully deleted expired coupon: " . $coupon->getCode() . "\n";
    } catch (Exception $e) {
        echo "Failed to delete coupon " . $coupon->getCode() . ": " . $e->getMessage() . "\n";
    }
}
?>

Key Changes from Your Original Script

  • Target Coupons, Not Rules: We load the salesrule/coupon collection instead of sales rules, and filter by your specific rule ID. This ensures we only touch coupons linked to your newsletter subscription rule.
  • Check Coupon-Specific Expiry: Each coupon generated by your script has its own to_date, so we compare that directly to today instead of using the rule's expiry date.
  • Maintain Prefix Filter: Added the "XX" prefix filter to ensure you only delete coupons you generated, not other coupons tied to the same rule.
  • Error Handling: Added try/catch blocks to log issues during deletion and give clear feedback.

Important Notes

  1. Backup First: Always back up your database before running scripts that modify or delete data.
  2. Test in Staging: Run this in a staging environment first to confirm it works as expected.
  3. Dry Run: Use the optional dry run line to preview which coupons will be deleted before executing the actual deletion.

内容的提问来源于stack exchange,提问作者thomaslester

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:43:05