1

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!

  • I've not used magento so I'm thinking around what I see in your code. Once an instance of Mage is defined in $generator you can setData, when you need to set the date to an old expired date can you retrieve the array (i.e. is there a getData?) put it back into a variable, update the date and then setData once more? – garrettlynchirl Jan 20 '18 at 15:55
  • Thank you for your input. Unfortunately that is not what I looked for. I´m not trying to set an expire date. My challenge is to find the code that will let me delete coupon codes contained in the rule. – thomaslester Jan 20 '18 at 19:56
  • "coupon codes contained in the rule" - where are these saved, in a database, in a cookie? – garrettlynchirl Jan 21 '18 at 12:09
  • in the DB table salesrule_coupon. I post the solution I finally made below. – thomaslester Jan 21 '18 at 17:09

1 Answers1

1

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);
   }
}
}
  • ok. It was very unclear that mysql was part of the question you originally asked. I do not know Magento (and i suspect that not many do as your question would have got more replies) but I do know mysql, lots of people do, and could have helped more quickly if this was mentioned. Anyway glad you found a solution, it looks good to me. :) – garrettlynchirl Jan 22 '18 at 14:47
  • I´m sorry that I wasn´t clear. I already had the solution in pure SQL, but that is to unsafe to have files around with SQL creditetials in them. I hoped for a solution that did not include SQL at all. That was also the reason that I posted the code that delete sales rules in Magento. I hoped for help to modify that script so it would delete the unique coupon codes in the Shopping Cart Rule - not the whole rule. My solution works, but I still would like to know if the same can be done without the Mage::getSingleton script and query. And look more like the 2nd script in my post. – thomaslester Jan 22 '18 at 22:15