使用Magento后台手动生成优惠券,效率太低了。如果生成1万个号码不一样的优惠券,还是要靠代码。
我来分享一段从互联网获得的批量生成优惠券的代码,如下:
此代码在Magento外部运行,所有使用了require_once(‘app/Mage.php’); 此代码只是一个生成优惠券的演示,如果要实现具体的功能,还需要修改,但是有了这个demo就容易多了。
示例1:
Php代码
<?php
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 coupan 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';
//获得Magento优惠券的数据结构可以使用
Php代码 收藏代码
$coupon = Mage::getModel('salesrule/rule')->load(1);
echo '<pre>';
print_r($coupon->getData());
echo '</pre>';
示例2:
Php代码
$coupon = Mage::getModel('salesrule/rule');
$coupon->setName('test coupon')
->setDescription('this is a description')
->setFromDate('2010-05-09')
->setCouponCode('CODENAME')
->setUsesPerCoupon(1)
->setUsesPerCustomer(1)
->setCustomerGroupIds(array(1)) //an array of customer grou pids
->setIsActive(1)
//serialized conditions. the following examples are empty
->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";}')
->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";}')
->setStopRulesProcessing(0)
->setIsAdvanced(1)
->setProductIds('')
->setSortOrder(0)
->setSimpleAction('by_percent')
->setDiscountAmount(10)
->setDiscountQty(null)
->setDiscountStep('0')
->setSimpleFreeShipping('0')
->setApplyToShipping('0')
->setIsRss(0)
->setWebsiteIds(array(1));
$coupon->save();
(责任编辑:最模板) |