I have made a small module which I am testing which programmatically creates coupon codes based on a default coupon which gives customers 5% off the total cart.
The coupons are for one use and by one customer. The coupon generates fine and is visible under Promotions > Shopping Cart Price Rules and the rule name is the same as the coupon code.
The problem I'm having is that I want it to create the coupon but not to apply it to the basket UNTIL the customer enters the coupon code in the Discount Codes field on the frontend but instead when i add a product to the basket, the coupon applies the discount.
My question is how do I stop it adding the coupon automatically.
If there is a better way of creating coupon codes and rules programmatically, I would like to know.
My code if it matters is below:
public function createCoupon() {
// Get the rule in question
$rule = Mage::getModel('salesrule/rule')->load(1); //1 = ID of coupon in question
// Define a coupon code generator model instance
// Look at Mage_SalesRule_Model_Coupon_Massgenerator for options
$generator = Mage::getModel('salesrule/coupon_massgenerator');
$parameters = array(
'count'=> 1,
'format'=> 'alphanumeric',
'dash_every_x_characters'=> 4,
'prefix'=> 'XXX-',
'suffix'=> '-CODE5',
'length'=> 8
);
if( !empty($parameters['format']) ) {
switch( strtolower($parameters['format']) ) {
case 'alphanumeric':
case 'alphanum':
$generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC );
break;
case 'alphabetical':
case 'alpha':
$generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL );
break;
case 'numeric':
case 'num':
$generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC );
break;
}
}
$generator->setDash( !empty($parameters['dash_every_x_characters'])? (int) $parameters['dash_every_x_characters'] : 0);
$generator->setLength( !empty($parameters['length']) ? (int) $parameters['length'] : 6);
$generator->setPrefix( !empty($parameters['prefix']) ? $parameters['prefix'] : '');
$generator->setSuffix( !empty($parameters['suffix']) ? $parameters['suffix'] : '');
// Set the generator, and coupon type so it's able to generate
$rule->setCouponCodeGenerator($generator);
$rule->setCouponType( Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO );
// Get as many coupons as you required
$count = !empty($parameters['count'])? (int) $parameters['count'] : 1;
$codes = array();
for( $i = 0; $i < $count; $i++ ) {
$coupon = $rule->acquireCoupon();
$code = $coupon->getCode();
$codes[] = $code;
}
return $codes[0];
}
public function createCouponForLike($couponCode) {
$model = Mage::getModel('salesrule/rule');
$model->setName($couponCode);
$model->setDescription('Discount coupon for liking us on Facebook.');
$model->setFromDate(date('Y-m-d'));
$model->setCouponCode($couponCode);
$model->setUsesPerCoupon(1);
$model->setUsesPerCustomer(1);
$model->setCustomerGroupIds('0,1');
$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: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";}');
$model->setStopRulesProcessing(0);
$model->setIsAdvanced(1);
$model->setProductIds('');
$model->setSortOrder(1);
$model->setSimpleAction('by_percent');
$model->setDiscountAmount(5);
$model->setDiscountStep(0);
$model->setSimpleFreeShipping(0);
$model->setTimesUsed(0);
$model->setIsRss(0);
$model->setWebsiteIds('1');
$model->save();
}