tags:

views:

28

answers:

1

Hi,

Thanks in advance,

Here is my code which create the coupan code on fly.

$productId  = (int) $this->getRequest()->getParam('id'); 
        $discountprice=$_POST['product']['discountprice']; 
        $model = Mage::getModel('salesrule/rule');
        $couponCode=generateUniqueId(8);
        $model->setName($couponCode);
        $model->setCouponCode($couponCode);
        $model->setDescription('Discount coupon for Surger.');
        $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($productId);
        $model->setSortOrder(1);
        $model->setSimpleAction('by_percent');
        $model->setDiscountAmount($discountprice);
        $model->setDiscountStep(0);
        $model->setSimpleFreeShipping(0);
        $model->setTimesUsed(0);
        $model->setIsRss(0);
        $model->setWebsiteIds('1');
        $model->save();

but when i checkout particular product the discount apply automatically,my requirment is the discount must be apply after i enter the code in Discount Codes box,also when i enter the code in Discount Codes it shows the code is not valid...

Please help anyone and sorry for my english, I am also trying my best to solve this if i find any solution i'll put here.

Thanks again,

Jitendra Dhobi

A: 

Hi guys, thanks for viewing my question the answer code is here u can set the coupan code your self and apply it while checkout.

function generateUniqueId($length = null)
    {
        $rndId = crypt(uniqid(rand(),1));
        $rndId = strip_tags(stripslashes($rndId));
        $rndId = str_replace(array(".", "$"),"",$rndId);
        $rndId = strrev(str_replace("/","",$rndId));
            if (!is_null($rndId)){
            return strtoupper(substr($rndId, 0, $length));
            }
        return strtoupper($rndId);
    }
    /* create unique coupan code */

        $productId  = (int) $this->getRequest()->getParam('id'); 
        $discountprice=$_POST['product']['discountprice']; 
        $model = Mage::getModel('salesrule/rule');
        $couponCode=generateUniqueId(8);
        $model->setName($couponCode);
        $model->setDescription('Discount coupon for Surger.');
        $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($productId);
        $model->setSortOrder('0');
        $model->setSimpleAction('by_percent');
        $model->setDiscountAmount($discountprice);
        $model->setDiscountStep(0);
        $model->setSimpleFreeShipping(0);
        $model->setCouponType(2);
        $model->setCouponCode($couponCode);
        $model->setUsesPerCoupon(1);
        $model->setTimesUsed(0);
        $model->setIsRss(0);
        $model->setWebsiteIds('1');
        $model->save();
Jitendra