This is a working example I just finished for a client. The hardest part which is not well documented is the addition of the Sales Action Condition. The serialization entries do NOTHING. You have to add them as a separate object.
I'll start with the screen shots of what I am trying to accomplish:
<?php
ini_set("memory_limit","124M");
require 'shop/app/Mage.php';
Mage::app();
Mage::register('isSecureArea', true);
$codes = Array(
'MASTER-RMA-CA5-1' => '1',
'MASTER-RMA-CA5-2' => '2',
'MASTER-RMA-CA5-3' => '3',
'MASTER-RMA-CA5-4' => '4',
'MASTER-RMA-CA5-6' => '6',
'MASTER-RMA-CA5-7' => '7',
'MASTER-RMA-CA5-8' => '8',
'MASTER-RMA-CA5-9' => '9'
);
$keys = array_keys($codes);
foreach ($keys as &$key) {
createCouponForRMA($key, $codes[$key]);
}
function createCouponForRMA($couponCode, $discountValue) {
echo "Code: $couponCode \n\r";
echo "Value: $discountValue \n\r";
$model = Mage::getModel('salesrule/rule');
$model->setName($couponCode);
$model->setDescription('RMA Coupon Code');
$model->setFromDate(date('Y-m-d'));
$model->setToDate(NULL);
$model->setCouponType(2);
$model->setCouponCode($couponCode);
$model->setUsesPerCoupon(0);
$model->setUsesPerCustomer(0);
$model->setCustomerGroupIds('0,1,2,3,4');//0: not logged in, 1: general
$model->setIsActive(1);
$model->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\";}');
$model->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:3:\"!{}\";s:5:\"value\";s:17:\"77, 176, 185, 242\";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:402:\"S6MMDM-168, S6MMDM-166, S6MMDM-167, S6MMDM-165, S6MMDM-164, S6MMDZ-163, SIDCDZ-013-8, SIDCDZ-014-11.5, SIDCDZ-014-11, SIDCDZ-014-10.5, SIDCDZ-014-10, SIDCDZ-014-9.5, SIDCDZ-014-9, SIDCDZ-014-8.5, SIDCDZ-014-8, SIDCDZ-014, SIDCDZ-013, SIDCDZ-013-13, SIDCDZ-013-12, SIDCDZ-013-9.5, SIDCDZ-013-11.5, SIDCDZ-013-11, SIDCDZ-013-10.5, SIDCDZ-013-10, SIDCDZ-013-9, SIDCDZ-013-8.5, SIDCDZ-014-13, SIDCDZ-014-12\";s:18:\"is_value_processed\";b:0;}}}');
$model->setStopRulesProcessing(0);
$model->setIsAdvanced(1);
$model->setProductIds('');
$model->setSortOrder(0);
$model->setSimpleAction('cart_fixed');
$model->setDiscountAmount($discountValue);
$model->setDiscountStep(0);
$model->setSimpleFreeShipping(2);
$model->setTimesUsed(0);
$model->setIsRss(0);
$model->setWebsiteIds('5'); //1:US, 5:CA
$ids = '77, 176, 185, 242'; // Put your category ids here
$skuCond = Mage::getModel('salesrule/rule_condition_product')
->setType('salesrule/rule_condition_product')
->setAttribute('category_ids')
->setOperator('!{}')
->setValue($ids);
$model->getActions()->addCondition($skuCond);
$skus = 'S6MMDM-168, S6MMDM-166, S6MMDM-167, S6MMDM-165, S6MMDM-164, S6MMDZ-163, SIDCDZ-013-8, SIDCDZ-014-11.5, SIDCDZ-014-11, SIDCDZ-014-10.5, SIDCDZ-014-10, SIDCDZ-014-9.5, SIDCDZ-014-9, SIDCDZ-014-8.5, SIDCDZ-014-8, SIDCDZ-014, SIDCDZ-013, SIDCDZ-013-13, SIDCDZ-013-12, SIDCDZ-013-9.5, SIDCDZ-013-11.5, SIDCDZ-013-11, SIDCDZ-013-10.5, SIDCDZ-013-10, SIDCDZ-013-9, SIDCDZ-013-8.5, SIDCDZ-014-13, SIDCDZ-014-12'; // Put your product SKU here
$skuCond2 = Mage::getModel('salesrule/rule_condition_product')
->setType('salesrule/rule_condition_product')
->setAttribute('sku')
->setOperator('!{}')
->setValue($skus);
$model->getActions()->addCondition($skuCond2);
try {
$model->save()->getId();
echo "SUCCESS: " + $couponCode;
} catch (Exception $e) {
echo "FAILED: $couponCode \n\r";
}
}
?>






