即时创建的Magento折扣优惠券代码,但无法正常运行 [英] Magento discount coupon code created on fly but not working properly
问题描述
这是我的代码,可以即时创建优惠券代码:
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.
推荐答案
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张优惠券的代码.
This is my code to create 1000 coupons.
-
放入$ model-> save();循环外?
put $model->save(); outside the loop?
您应该设置正确的$ productId
you should set a right $productId
这篇关于即时创建的Magento折扣优惠券代码,但无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!