<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');
Mage::register('isSecureArea', 1);
class generateCouponCode{
protected $_file = 'coupon_code.csv';
protected $_count = 0;
public function csvToArray($_file) {
$csvArray = array();
$file = fopen($_file, "r");
fgetcsv($file);
while (!feof($file)) {
$values = fgetcsv($file);
if ($values) {
$csvArray[] = $values;
}
}
return $csvArray;
}
public function getAllCustomerGroups() {
//get all customer groups
$customerGroups = Mage::getModel('customer/group')->getCollection();
$groups = array();
foreach ($customerGroups as $group) {
$groups[] = $group->getId();
}
return $groups;
}
public function getAllWbsites() {
//get all wabsites
$websites = Mage::getModel('core/website')->getCollection();
$websiteIds = array();
foreach ($websites as $website) {
$websiteIds[] = $website->getId();
}
return $websiteIds;
}
//read comments for each line
public function generateRule($uniqueId) {
$conditionProduct = Mage::getModel('salesrule/rule_condition_product')
->setType('salesrule/rule_condition_product')
->setAttribute('sku')
->setOperator('==')
->setValue('Bab0001');
/** @var Mage_SalesRule_Model_Rule_Condition_Product_Found $conditionProductFound */
$conditionProductFound = Mage::getModel('salesrule/rule_condition_product_found')
->setConditions(array($conditionProduct));
/** @var Mage_SalesRule_Model_Rule_Condition_Combine $condition */
$condition = Mage::getModel('salesrule/rule_condition_combine')
->setConditions(array($conditionProductFound));
$rule = Mage::getModel('salesrule/rule');
$rule->setName($uniqueId);
$rule->setCouponType('2');
$rule->setDescription('');
$rule->setFromDate('2012-10-05');//date('Y-m-d')
$rule->setToDate('2013-10-05');//date('Y-m-d')
$rule->setCouponCode($uniqueId);
$rule->setUsesPerCoupon(1); //number of allowed uses for this coupon
$rule->setUsesPerCustomer(1); //number of allowed uses for this coupon for each customer
$rule->setCustomerGroupIds($this->getAllCustomerGroups());
$rule->setIsActive(1);
$rule->setStopRulesProcessing(0); //set to 1 if you want all other rules after this to not be processed
$rule->setIsRss(0); //set to 1 if you want this rule to be public in rss
$rule->setIsAdvanced(1); //have no idea what it means :)
$rule->setProductIds('');
$rule->setSortOrder(0); // order in which the rules will be applied
$rule->setSimpleAction('cart_fixed');
$rule->setDiscountAmount('29.98');
$rule->setDiscountQty(0); //Maximum Qty Discount is Applied to
$rule->setDiscountStep(0); //used for buy_x_get_y; This is X
$rule->setSimpleFreeShipping(0); //set to 1 for Free shipping
$rule->setApplyToShipping(0); //set to 0 if you don't want the rule to be applied to shipping
$rule->setWebsiteIds($this->getAllWbsites());
$rule->setConditionsSerialized(serialize($condition->asArray()));
$rule->setConditions($condition);
$rule->loadPost($rule->getData());
try
{
$rule->save();
Mage::log('Coupon code created for '.$uniqueId.'', null, 'couponcode.log');
$this->_count++;
}
catch (Exception $e)
{
echo 'Caught exception for coupon code '.$uniqueId.':: ', $e->getMessage(), "\n";
Mage::log('Caught exception for coupon code '.$uniqueId.'::'.$e->getMessage().'', null, 'couponcode.log');
}
}
public function importCsv() {
$file = Mage::getBaseDir('base').DS.$this->_file;
if (empty($file)) {
throw new Exception("You need to upload a file to import.");
}
$csv = trim(file_get_contents($file));
if (empty($csv)) {
throw new Exception("The file is empty.");
}
$csv = $this->csvToArray($file);
foreach ($csv as $key => $row) {
$this->generateRule($row[0]);
}
echo sprintf("total %s coupon code created",$this->_count);
}
}
$obj = new generateCouponCode();
$obj->importCsv();
?>