2010-10-27 148 views
1

這裏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
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. 之外,你應該設定一個正確的$的productId

+0

小記:$模型 - > setUsesPerCoupon(1)在代碼中增加了一倍。 – Alekc 2013-01-11 09:42:09

0

感謝您查看我的問題答案代碼在這裏你可以設置你的自己的coupan代碼,並在結帳時應用它。

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(); 
相關問題