How to delete expired coupon automatically in magento

2 Sep

When it comes to the use of coupon code in your shopping cart. You will see that there will be a long list and lots are already expired but consuming space in database for no reason. It also happened with me. Saw a lot of useless coupon code and then decided to delete all those coupon code.

Yes ofcourse, you cannot think of doing it manually. Here is the code snippet that will do the trick for you. Let understand what it does?

<?php
//Including the Mage.php to run the process
require_once(‘app/Mage.php’);
Mage::app(‘default’);

// Load the salesrule Model
$allCoupons = Mage::getModel(‘salesrule/rule’)->getCollection()->load();
$noofCoupons = 0;

//It takes the current time

$Now = strtotime(‘now’);

$today = date(“Y-m-d”, $Now);

// Use foreach to get all the coupon details and do as required.

foreach ($allCoupons as $aCoupon) {
$couponName = $aCoupon->getName(); // Get the name of coupon
$subString = substr($couponName,0,16);
$expiryDate = $aCoupon->getToDate(); // Get the expiery date of coupon
$expiryDay = date(“Y-m-d”, $expiryDate);
// This will delete the coupon that satisfy the if condition.
if(($subString == “Abandoned coupon”) && ($today > $expiryDate)) {
$aCoupon->delete();
}
}
?>

You are done. All the expired coupons are deleted. Coupon tables are happy now.. 🙂

Advertisements

One Response to “How to delete expired coupon automatically in magento”

  1. jim September 30, 2013 at 8:19 pm #

    Hello, everyone
    It is a great change to increase customer loyalty and involve customer to buy more. Of course partically each person use social networks today. Now I use Facebook and Twitter Promo. Seems that it is good and meets my requirements.
    Hope it will be useful for someone.
    Also the extension can automatically delete coupons, if customer dislike the page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: