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
#2013-05-29
#David Boyce

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

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

Mage::app();

#Free Post
$code = "[PROMOTIONCOUPONCODE]";
$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');
  $coupon->setName($name)
  ->setDescription($name)
  ->setFromDate($from_date)
  ->setToDate($to_date)
  ->setCouponCode($code)
  ->setUsesPerCoupon('')
  ->setUsesPerCustomer('')
  ->setCustomerGroupIds(getAllCustomerGroups()) //an array of customer group ids
  ->setIsActive(1)

  //serialized conditions.  the following examples are empty
  ->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";}')

  ->setStopRulesProcessing(0)
  ->setIsAdvanced(1)
  ->setProductIds('')
  ->setSortOrder(0)
  ->setSimpleAction('cart_fixed')
  ->setDiscountAmount($amount)
  ->setDiscountQty(null)
  ->setDiscountStep('0')
  ->setSimpleFreeShipping($freeShipping)
  ->setApplyToShipping('0')
  ->setIsRss(0)
  ->setWebsiteIds(getAllWbsites())
  ->setCouponType(2)
  ->setStoreLabels($labels)
  ;

  $coupon->save();

}


function getAllCustomerGroups(){
  //get all customer groups
  $customerGroupsCollection = Mage::getModel('customer/group')->getCollection();
  $customerGroupsCollection->addFieldToFilter('customer_group_code',array('nlike'=>'%auto%'));
  $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 *