0
votes

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();
} 
1
Remember to add a job which cleans old used ones out though or you'll end up with huge numbers of shopping cart price rules which can murder the speed of the add to cart action.McNab

1 Answers

0
votes

I'm guessing you need to add a setting for coupon type, try this:

// The value can be 1 or 2
// If the value is set to 1 the rule will apply without the need of a coupon
$model->setCouponType(2);

See this guide for more information, actually there's quite a lot of tutorials out there: http://www.demacmedia.com/magento-commerce/mini-tutorial-creating-shopping-cart-rules-programatically/

EDIT Noticed to late that you already set it at one point with this constant:

Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO

Problem there could probably be that the only mention I can find of COUPON_TYPE_AUTO says it's set to 3, when you probably want it to be set to 2.