开发者

Magento Set discount code for specific product on fly

开发者 https://www.devze.com 2023-01-22 13:08 出处:网络
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 changin

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-&开发者_Go百科gt;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();

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


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);


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消