Magento 为特定产品即时设置折扣代码

发布于 2024-09-29 16:01:06 字数 5845 浏览 1 评论 0原文

预先感谢..

正如在我之前的问题中一样,我已经为产品设置了折扣代码,其工作正常,但我想仅将此规则应用于特定产品,这可以通过更改以前的代码来实现。

请帮助我...

谢谢, Jitendra

供您参考,代码在这里....

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

这是将折扣应用于特定产品的完美代码..我找到了解决方案..该代码对于将折扣应用于特定产品而言工作正常。

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

任何人都知道如何对一种产品应用此折扣,只有在以下情况下发生的情况是折扣总计,但我想将此折扣应用于我已输入折扣代码的该产品,任何人都可以帮助我分离此折扣。

请帮忙...

谢谢, 吉滕德拉

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-10-06 16:01:06

根据我对折扣系统的了解,您需要在保存规则之前执行以下操作。正如您所看到的,它的工作原理是搜索具有特定 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);

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);
慵挽 2024-10-06 16:01:06

这是我自己问题的答案:

任何人都知道如何对一种产品应用此折扣,只有在以下情况下发生的情况是折扣总计,但我想将此折扣应用于我输入折扣代码的该产品谁能帮我分开这个折扣。 ???

谢谢,
吉腾德拉·多比

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文