Magento折扣优惠券代码在飞行中创建但无法正常工作

时间:2010-10-27 07:32:20

标签: magento

以下是我的代码,可以动态创建优惠券代码:

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

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

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

2 个答案:

答案 0 :(得分:2)

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-&gt; save();在圈外?

  2. 你应该设置一个正确的$ productId

答案 1 :(得分:0)

感谢查看我的问题答案代码在这里你可以设置你自己的偶然代码并在结账时应用它。

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