thomaslester
thomaslester

Reputation: 41

How to delete expired coupon codes IN a shopping cart price rule? (DB Table salesrule_coupon)

I use a script generating a unique coupon code in a specific shopping cart price rule when someone signs up for a newsletter.

I would now like to delete expired coupons codes in that rule and not the rule it self. The only script I found here is deleting the whole rule.

How can I use MAGE:: to delete expired entries generated by the script below?

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

//Initializes Mage
Mage::app('admin');


$todaysdateis = date('Y-m-d', strtotime('+14 days'));
$generator = Mage::getModel('salesrule/coupon_massgenerator');
$data = array
(
'max_probability'   => .25,
'max_attempts'      => 10,
'uses_per_customer' => 1,
'uses_per_coupon'   => 1,
'qty'               => 1, //number of coupons to generate
'length'            => 3, //length of coupon string
'to_date'           => "$todaysdateis", //ending date of generated promo
'prefix'            => $prefix,
'format'          => Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC,
'rule_id'         => $rule_id //the id of the shopping cart rule you will use 
as a template
 );

 $generator->validateData($data);
 $generator->setData($data);
 $generator->generatePool();

The code I found that will delete the RULE and not only the expired Coupons in the Rule is below. How can I adjust that so I delete the individual expired coupon codes contained in the Rule?

<?php 
require_once('app/Mage.php');
Mage::app('default');

$allCoupons = Mage::getModel('salesrule/rule')->getCollection()->load();
$rightNow = strtotime('now');
$today = date("Y-m-d", $rightNow);

foreach ($allCoupons as $aCoupon) {
$couponName = $aCoupon->getName();
$subString = substr($couponName,0,16);
$expiryDate = $aCoupon->getToDate();
$expiryDay = date("Y-m-d", $expiryDate);

if(($subString == "XX") && ($today > $expiryDate)) { 
$aCoupon->delete();
}
}

?>

Looking forward to hear from you!

Upvotes: 1

Views: 1041

Answers (1)

thomaslester
thomaslester

Reputation: 41

Answering my own question. I found a solution that works. But is there a better one / more safe one?

$rightNow = strtotime('now');
$today = date("Y-m-d", $rightNow);

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


$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$salesrule = Mage::getSingleton('core/resource')->getTableName('salesrule_coupon');

$ruleid = '51';

$query = "SELECT * FROM salesrule_coupon WHERE rule_id ='".$ruleid."'";

$results = $read->fetchAll($query);

if ($results){
foreach($results as $result) {

if ($result['expiration_date'] < $today) {
    $querydel = "DELETE FROM salesrule_coupon WHERE coupon_id ='".$result['coupon_id']."'";

    $writeConnection->query($querydel);
   }
}
}

Upvotes: 1

Related Questions