Creating Magento Free Post Promotion Coupons through code

I’ve getting increasing numbers of promotions to set-up in Magento, so I decided it was time to write a script so I can get them added more quickly.

I found some good code here, and decided to adapt it to suit my needs.

This script will generate a free postage code, with start and expiry date but could be easily adapted to offer discounts. It turned a 30 min job into a 5 min one. I’ll eventually write something in VBA so I can generate the data in excel and then just push the button to update the website with new promotions.

#Free Post Coupon Generation
#David Boyce

define('MAGENTO', realpath('/srv/www/htdocs/Catalogue'));
ini_set('memory_limit', '128M');

require_once MAGENTO . '/app/Mage.php';


#Free Post
$label = "[PROMOTIONLABEL]";
$amount = 0;
$freeShipping = 1;
$from_date = "2013-05-29";
$to_date = "2013-07-01";

generateRule( $code, $amount, $label, $from_date, $to_date, $name, $freeShipping);

function generateRule($code, $amount, $label, $from_date, $to_date, $name = '', $freeShipping){

  $name = (empty($name))? $label : $name;
  $labels[0] = $label;//default store label

  $coupon = Mage::getModel('salesrule/rule');
  ->setCustomerGroupIds(getAllCustomerGroups()) //an array of customer group ids

  //serialized conditions.  the following examples are empty




function getAllCustomerGroups(){
  //get all customer groups
  $customerGroupsCollection = Mage::getModel('customer/group')->getCollection();
  $groups = array();
  foreach ($customerGroupsCollection as $group){
    $groups[] = $group->getId();
  return $groups;

function getAllWbsites(){
  //get all wabsites
  $websites = Mage::getModel('core/website')->getCollection();
  $websiteIds = array();
  foreach ($websites as $website){
    $websiteIds[] = $website->getId();
  return $websiteIds;

Leave a Reply

Your email address will not be published. Required fields are marked *