Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Create multiple coupon code based on script or sql
 
raym0nd
Member
 
Total Posts:  45
Joined:  2010-06-23
 

Hi,

Is there a convenient method for me to create shopping cart price rules with coupon codes through scripts or sql?

Every month I am require to create a new coupon code. Basically, the rule settings is the same except the coupon code and the from and to date field.

Please advise if I can create a script or SQL statement so that every month I can just edit the script values and run.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello
You can manually create a discount rule with the desired settings.
After saving you will have the ID of the rule.
To duplicate the rule you can run the following script

$demoRuleId 55;//replace with the id you got
$rule Mage::getModel('salesrule/rule')->load($demoRuleId);
$rule->setId(null); //reset the id so it is saved as a new rule
$rule->setCouponCode('YOUR NEW COUPON CODE');//change the coupon code
$rule->setFromDate('2013-03-22');//start of the promotion
$rule->setToDate('2013-03-22');//end of the promotion
$rule->save();

Cheers,
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
raym0nd
Member
 
Total Posts:  45
Joined:  2010-06-23
 
tzyganu - 22 March 2013 02:58 AM

Hello
You can manually create a discount rule with the desired settings.
After saving you will have the ID of the rule.
To duplicate the rule you can run the following script

$demoRuleId 55;//replace with the id you got
$rule Mage::getModel('salesrule/rule')->load($demoRuleId);
$rule->setId(null); //reset the id so it is saved as a new rule
$rule->setCouponCode('YOUR NEW COUPON CODE');//change the coupon code
$rule->setFromDate('2013-03-22');//start of the promotion
$rule->setToDate('2013-03-22');//end of the promotion
$rule->save();

Cheers,
Marius.

Hi tzyganu,

Thanks for the reply.

I understand that the demoRuleId is got from salesrule table. However, I need some help on using the script provided.

I’m quite new to magento platform and I would appreciate if you can advise me on how should I go about copying the script to my server and also what types of class that need to be imported.

Lastly, I would like to confirm whether is there any other database table need to be considered when adding this coupon code manually as I’m afraid there might data referencing issue.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

You can put the script in one of your models and configure a cron to run your model: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/how_to_setup_a_cron_job

 
Magento Community Magento Community
Magento Community
Magento Community
 
pablodias
Jr. Member
 
Total Posts:  2
Joined:  2013-04-03
 

Hi Marius –

Your tip is great and almost worked for me, except it’s changing the Coupon Code field on the loaded rule.
By the way, it’s creating a new rule without Coupon Code field.

I tried to duplicate and then load again the $model->getId() from the new inserted rule, but doesn’t work.
Also tried to clean loaded data before save, but it stills changing the loaded rule.

Am I missing anything?
Here’s my standalone code:

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

$demoRuleId 217;
$model Mage::getModel('salesrule/rule');
$coupon $model->load($demoRuleId);

$coupon->setId(null);
$coupon->setFromDate('2013-04-03');
$coupon->setToDate('2014-07-31');

$model->save();
$newID $model->getId();

$coupon $model->load($newID);
$coupon->setId($newID);
$coupon->setName('TESTCOUPON');
$coupon->setCouponCode('TESTCOUPON');

$coupon->save();
?>

Your help is appreciated.
Thanks in advance!

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Ok. Try this code. It worked for me:

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

$demoRuleId 217;
$model Mage::getModel('salesrule/rule');
$coupon $model->load($demoRuleId);//load existing rule
$newCoupon Mage::getModel('salesrule/rule');//create new rule
$newCoupon->setData($coupon->getData());//copy the data from old rule to new rule
$newCoupon->unsRuleId();//unset the rule id so it's considered new
$newCoupon->setFromDate('2013-04-03');//change from date
$newCoupon->setToDate('2014-07-31');//change to date
$newCoupon->setName('New');//change the rule name
$newCoupon->setCouponCode('New'); //change the rule coupon
$newCoupon->save();//save new rule

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
pablodias
Jr. Member
 
Total Posts:  2
Joined:  2013-04-03
 

Perfect, Marius.
Your solution works like a charm!

Many thanks,
Pablo Dias

 
Magento Community Magento Community
Magento Community
Magento Community
 
raym0nd
Member
 
Total Posts:  45
Joined:  2010-06-23
 

Thanks for the updated script.

How do I copy the action and condition as well?

I have researched online they used

>setConditionsSerialized('a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')
    ->
setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}')

This seems to be not working for other user and me.

Please advise.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

@raym0nd: The code above copies the rule conditions and the rule actions as well.
What did not work for you?
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
raym0nd
Member
 
Total Posts:  45
Joined:  2010-06-23
 
tzyganu - 04 April 2013 10:40 PM

@raym0nd: The code above copies the rule conditions and the rule actions as well.
What did not work for you?
Marius.

Hi Marius,

Below is my code, which every data is able to go in except the action and condition.

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

$demoRuleId 470;
$model Mage::getModel('salesrule/rule');
$coupon $model->load($demoRuleId);//load existing rule
$newCoupon Mage::getModel('salesrule/rule');//create new rule
$newCoupon->setData($coupon->getData());//copy the data from old rule to new rule
$newCoupon->unsRuleId();//unset the rule id so it's considered new
$newCoupon->setFromDate('2013-04-01');//change from date
$newCoupon->setToDate('2014-05-31');//change to date
$newCoupon->setName('NewRule');//change the rule name
$newCoupon->setCouponCode('NewCoupon'); //change the rule coupon
$newCoupon->setConditionsSerialized('a:7:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:1:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_address";s:9:"attribute";s:9:"total_qty";s:8:"operator";s:2:">=";s:5:"value";s:1:"3";s:18:"is_value_processed";b:0;}}}');
$newCoupon->setActionsSerialized('a:7:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:2:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_product";s:9:"attribute";s:12:"category_ids";s:8:"operator";s:2:"{}";s:5:"value";s:10:"10, 23, 20";s:18:"is_value_processed";b:0;}i:1;a:5:{s:4:"type";s:32:"salesrule/rule_condition_product";s:9:"attribute";s:3:"sku";s:8:"operator";s:3:"!()";s:5:"value";s:18:"15-00005, 15-00249";s:18:"is_value_processed";b:0;}}}');    
$newCoupon->save();//save new rule
?>

The record in the database is the default value:

conditions_serialized column:
a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";b:1;s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}
actions_serialized column
:
a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";b:1;s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}

My conditions basically check the minimum quantity and the action is to check for category and sku rule.

Please advise whether is possible to insert this two rules via the script.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

If you are trying to change the conditions and/or actions while duplicating the rule, maybe duplicating is not the best choice.
Take a look at this article that describes how to create bulk coupons with a script. It also describes how to add conditions.
http://marius-strajeru.blogspot.ro/2010/04/create-bulk-discount-rules.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
raym0nd
Member
 
Total Posts:  45
Joined:  2010-06-23
 
tzyganu - 04 April 2013 11:26 PM

If you are trying to change the conditions and/or actions while duplicating the rule, maybe duplicating is not the best choice.
Take a look at this article that describes how to create bulk coupons with a script. It also describes how to add conditions.
http://marius-strajeru.blogspot.ro/2010/04/create-bulk-discount-rules.html

I tried the code on post #5 but it is not able to duplicate the actions and conditions.

I just visited the URL, it seems to have more coding and it does not have coding for action rule.

Not sure if it is better if I use SQL to duplicate both condition and action rule?

Please advise.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

The script worked for me without any problems.
Sorry I have no other ideas.

 
Magento Community Magento Community
Magento Community
Magento Community
 
raym0nd
Member
 
Total Posts:  45
Joined:  2010-06-23
 

I have added my action conditions based on the codes below.

It will apply the rule matching the following conditions:
if all of these conditions are true:
category contains 11 & 22 (category id)
SKU is not one of 12345 & 12346.

The attribute and operator can be extracted from the HTML dropdown box value.

The following code might not be the correct/best way to achieve but just thought of sharing it. Let me know if it can be improved.

$actions Mage::getModel('salesrule/rule_condition_product'
  ->
setType('salesrule/rule_condition_product')
  ->
setAttribute('category_ids'
  ->
setOperator('{}'
  ->
setValue('11,22');
$newCoupon->getActions()->addCondition($actions); 
$actions Mage::getModel('salesrule/rule_condition_product'
    ->
setType('salesrule/rule_condition_product')
    ->
setAttribute('sku')
    ->
setOperator('!()')
    ->
setValue('12345, 12346');
$newCoupon->getActions()->addCondition($actions);
 
Magento Community Magento Community
Magento Community
Magento Community
 
raym0nd
Member
 
Total Posts:  45
Joined:  2010-06-23
 

I have tested it is able to work. However, the database value got a bit different compared to the original record. I have bold the differences below.

Anyone know what is the difference about?

a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";b:1;s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}

a
:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";[b]s:1:"1[/b]";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}
 
Magento Community Magento Community
Magento Community
Magento Community
 
shelly_kelly
Jr. Member
 
Total Posts:  3
Joined:  2013-12-03
 

Hey folks,

I wanted to do the same - make unique coupons generated automatically. As I am not a developer, I installed the Promotional Coupons from Magalter. SO now my coupon codes are secure and I show them to targeted audience only.

You can read more at their website - http://www.magalter.com/promotional-coupons.html

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2