Tag Archives: Coupon

How to update expiry date of coupon in Magento

3 Oct

I have asked by one of my colleague to update the expiration date of coupon code in Magento, I thought of writing a script which will update the date of all coupon. Suddenly thought of a shortcut method and updated in database.
Here is the simple query:-
UPDATE salesrule_coupon set…

Advertisements

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

%d bloggers like this: