Magento 折扣优惠券代码即时创建但无法正常工作

发布于 2024-09-29 11:13:45 字数 1663 浏览 2 评论 0原文

这是我动态创建优惠券代码的代码:

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

但是当我结帐特定产品时折扣会自动应用,我的要求是折扣必须在我在折扣代码框中输入代码后以及当我在折扣代码显示代码无效...

请帮助任何人。我也在尽力解决这个问题;如果我找到任何解决方案,我会放在这里。

Here is my code which create the coupon code on the 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 requirement is the discount must be apply after I enter the code in the Discount Codes box, also when I enter the code in Discount Codes it shows the code is not valid...

Please help anyone. I am also trying my best to solve this; if I find any solution I'll put here.

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

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

发布评论

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

评论(2

停顿的约定 2024-10-06 11:13:45
require_once('app/Mage.php');
Mage::app('default');
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 coupon code */
for($cc = 0 ; $cc<15 ; $cc++){
    $productId  = 297;//(int) $this->getRequest()->getParam('id'); 
    $discountprice= 1;//$_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();
    }
    echo 'ok';

这是我创建 1000 张优惠券的代码。

  1. 把$model->save();循环外?

  2. you should set a right $productId

require_once('app/Mage.php');
Mage::app('default');
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 coupon code */
for($cc = 0 ; $cc<15 ; $cc++){
    $productId  = 297;//(int) $this->getRequest()->getParam('id'); 
    $discountprice= 1;//$_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();
    }
    echo 'ok';

This is my code to create 1000 coupons.

  1. put $model->save(); outside the loop?

  2. you should set a right $productId

︶ ̄淡然 2024-10-06 11:13:45

感谢您查看我的问题,答案代码在这里,您可以自行设置优惠券代码并在结帐时应用它。

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

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