tags:

views:

88

answers:

3

Hi,

Thanks in advance..

As in my previous question i have set the discount code for the product on fly its working fine but i want to apply this rule for specific product only is this possible by changing previous code.

please help me...

Thanks, Jitendra

for your information the code is here....

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();
+1  A: 

From what I can tell of the discount system you will need to do the following before saving the rule. As you can see it works by searching for any cart product with a specific SKU.

$sku = 'ABCD';            // Put your product SKU here
$found = Mage::getModel('salesrule/rule_condition_product_found')
         ->setType('salesrule/rule_condition_product_found')
         ->setValue(1)           // 1 == FOUND
         ->setAggregator('all'); // match ALL conditions
$model->getConditions()->addCondition($found);
$skuCond = Mage::getModel('salesrule/rule_condition_product')
           ->setType('salesrule/rule_condition_product')
           ->setAttribute('sku')
           ->setOperator('==')
           ->setValue($sku);
$found->addCondition($skuCond);
clockworkgeek
can u tell me how to apply this code actually i have
Jitendra
applied it but its not working i mean the execution process of script has stopped
Jitendra
please help me this code is not working or i am not applying it properly
Jitendra
I made a mistake when tidying up the snippet. It seems the order each condition is added in has an effect.
clockworkgeek
can u please elaborate more on your last comment.
Jitendra
My answer has been corrected so it should work better. Please try it again.
clockworkgeek
ya i have corrected my self and its working now
Jitendra
thanks for the help..:)
Jitendra
@Jitendra, you should accept the answer (click the check mark next to it) to award clockworkgeek points for providing the correct answer.
eyelidlessness
@eyelidlessness FYI i have put this answer my self please read the answer properly..
Jitendra
@Jitendra, Stack Overflow is a valuable resource partly because its users gain reputation for good questions and answers. It provides a direct way for questioners to accept answers for this reason: the person who provided you the correct answer is rewarded by gaining reputation points and becomes better able to participate in the site by doing so. Taking the answers provided by others and posting it yourself undermines the value of SO for its users.
eyelidlessness
@eyelidlessness:Please read the conversation below u'll understand what is going on,and the for the award concern i am ready to accept the question but where is the chekbox to accept it i cant see it after the any answer..
Jitendra
@clockworkgeek please say something
Jitendra
@eyelidlessness - It seems Jitendra has superseded my suggestion with a better answer. Twice! Thank you for your concern. Still, I had to correct my own snippet in case of another visitor attempting to use it.
clockworkgeek
@clockworkgeek:thanks
Jitendra
A: 

Hi,

Here is the perfect code for applying discount to specific product..I found the solution..This code is working fine for applying discount to specific product.

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'); 
        $sku=$this->getRequest()->getParam('sku');
        $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');

         $sku=$_POST['product']['sku'];
        print $sku;
/*$skuCond = Mage::getModel('salesrule/rule_condition_product')
           ->setType('salesrule/rule_condition_product')
           ->setAttribute('sku')
           ->setOperator('==')
           ->setValue($sku);
    print_r($skuCond); exit;       
$found = Mage::getModel('salesrule/rule_condition_product_found')
         ->setType('salesrule/rule_condition_product_found')
         ->setValue(1)           // 1 == FOUND
         ->setAggregator('all'); // match ALL conditions         

$model->loadPost($found);*/

$conditions = array();
$conditions[1] = array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
'new_child' => ''
);
$conditions['1--1'] = array
(
'type' => 'salesrule/rule_condition_product_found',//-> means 'if all of the following are true' - same rules as above for 'aggregator' and 'value'
//other values for type: 'salesrule/rule_condition_product_subselect' 'salesrule/rule_condition_combine'
'value' => 1,
'aggregator' => 'all',
'new_child' => '',
);

$conditions['1--1--1'] = array
(
'type' => 'salesrule/rule_condition_product',
'attribute' => 'sku',
'operator' => '==',
'value' => $sku,
);

$model->setData('conditions',$conditions);
$model->loadPost($model->getData());
$model->save();

anybody knows how to apply this discount for one product only what happens in following case is that the discount goes to grand total but i want to apply this discount to that product which i have entered the discount code can anybody help me to separate this discount.

please help...

thanks, Jitendra

Jitendra
It sounds like you need to use "catalogrule" instead of "salesrule".
clockworkgeek
is it possible in salesrule..?
Jitendra
Using salesrule automatically adds a line in the totals section. If you want to avoid that entirely and just reduce the price of a product directly then consider overriding it's price model. The price model is particular to a product type and has the method `getPrice()` called. Apply your reduction there instead of messing with discounts and your customers will see it's effects everywhere.
clockworkgeek
You are absolutely right but the requirement is that i have to do so using coupon code generation because the definition of
Jitendra
my assignment is such like this customer can be able to get the discount for particular product using discount code only.
Jitendra
@clockworkgeek Thanks for your answer but this is possible in shopping cart price rule see mynew code below i have done it.you just have to apply condition in the actions tab of shopping cart price rule.
Jitendra
A: 

This is the answer of my own question:

anybody knows how to apply this discount for one product only what happens in following case is that the discount goes to grand total but i want to apply this discount to that product which i have entered the discount code can anybody help me to separate this discount. ???

Thanks, Jitendra Dhobi

 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'); 
            $sku=$this->getRequest()->getParam('sku');
            $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');

             $sku=$_POST['product']['sku'];
            print $sku;
    /*$skuCond = Mage::getModel('salesrule/rule_condition_product')
               ->setType('salesrule/rule_condition_product')
               ->setAttribute('sku')
               ->setOperator('==')
               ->setValue($sku);
        print_r($skuCond); exit;       
    $found = Mage::getModel('salesrule/rule_condition_product_found')
             ->setType('salesrule/rule_condition_product_found')
             ->setValue(1)           // 1 == FOUND
             ->setAggregator('all'); // match ALL conditions         

    $model->loadPost($found);*/

    $conditions = array();
    $conditions[1] = array(
    'type' => 'salesrule/rule_condition_combine',
    'aggregator' => 'all',
    'value' => 1,
    'new_child' => ''
    );
    $conditions['1--1'] = array
    (
    'type' => 'salesrule/rule_condition_product_found',//-> means 'if all of the following are true' - same rules as above for 'aggregator' and 'value'
    //other values for type: 'salesrule/rule_condition_product_subselect' 'salesrule/rule_condition_combine'
    'value' => 1,
    'aggregator' => 'all',
    'new_child' => '',
    );

    $conditions['1--1--1'] = array
    (
    'type' => 'salesrule/rule_condition_product',
    'attribute' => 'sku',
    'operator' => '==',
    'value' => $sku,
    );

    $actions=array();
    $actions[1]=array(
    'type' => 'salesrule/rule_condition_product_combine',
    'aggregator' => 'all',
    'value' => 1,
    'new_child' => ''
    );
    //$actions['1--1'] = array
    //(
    //'type' => 'salesrule/rule_condition_product_found',//-> means 'if all of the following are true' - same rules as above for 'aggregator' and 'value'
    //other values for type: 'salesrule/rule_condition_product_subselect' 'salesrule/rule_condition_combine'
    //'value' => 1,
    //'aggregator' => 'all',
    //'new_child' => '',
    //);
    $actions['1--1'] = array
    (
    'type' => 'salesrule/rule_condition_product',
    'attribute' => 'sku',
    'operator' => '==',
    'value' => $sku,
    );

    //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:1:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_product";s:9:"attribute";s:3:"sku";s:8:"operator";s:2:"==";s:5:"value";s:6:"asdsad";s:18:"is_value_processed";b:0;}}}

    $model->setData('conditions',$conditions);
    $model->setData('actions',$actions);
    $model->loadPost($model->getData());
    $model->save();
Jitendra