November 30, 2015 HomeWP / Joomla! / Magento / Shopify / Drupal / Moodle / ZimbraMagento批量生成优惠券 Magento批量生成优惠券 By Gideon WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra 0 Comments 使用Magento后台手动生成优惠券,效率太低了。如果生成1万个号码不一样的优惠券,还是要靠代码。 我来分享一段从互联网获得的批量生成优惠券的代码,如下: 此代码在Magento外部运行,所有使用了require_once(‘app/Mage.php’); 此代码只是一个生成优惠券的演示,如果要实现具体的功能,还需要修改,但是有了这个demo就容易多了。 示例1: <?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优惠券的数据结构可以使用 $coupon = Mage::getModel('salesrule/rule')->load(1); echo '<pre>'; print_r($coupon->getData()); echo '</pre>'; 示例2: $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(); 此文参考文章地址:http://stackoverflow.com/questions/4030887/magento-discount-coupan-code-created-on-fly-but-not-working-properly/5894834#5894834 http://blog.leoware.com/magento/coupons-in-magento-part-2.html http://alanstorm.com/magento_model_generation 原文:www.hellokeykey.com 本文:Magento批量生成优惠券 Related Posts Shopify 英文(多国语言)国际网店 注册指南点击这里 快速注册 shopify 英文网店 可以获得14天免费试用,注册完成后 您可以设置您的网站,添加您产品,快速预览你的网店 , 开启国际电子商务之旅注册时注意以下几点 1… Shopify:管理一个顶级域名绑定shopify网店你可以进入你的域名管理界面- Domains page: 登陆你的shopify 管理员账户,, 点击 Online Store: 然后点击 Domains: 添加你的域名 这个时候你已经设置完成… Magento 数据库查询速记 select, insert, update, and delete dataquery.php <?php include_once 'app/Mage.php'; Mage::app(); //==================================================================== //$product = Mage::getModel('catalog/product')->load(1);… Related Posts BuddyPress安装指南 magento 设置ssl coufigure magento with ssl wordpress二次开发技巧-functions.php篇 About Author Gideon Add a Comment Cancel replyYour email address will not be published. Required fields are marked *Comment:*Name:* Email Address:* Website: Save my name, email, and website in this browser for the next time I comment. Time limit is exhausted. Please reload CAPTCHA. 9 − = seven Δ
使用Magento后台手动生成优惠券,效率太低了。如果生成1万个号码不一样的优惠券,还是要靠代码。
我来分享一段从互联网获得的批量生成优惠券的代码,如下:
此代码在Magento外部运行,所有使用了require_once(‘app/Mage.php’); 此代码只是一个生成优惠券的演示,如果要实现具体的功能,还需要修改,但是有了这个demo就容易多了。
示例1:
//获得Magento优惠券的数据结构可以使用
示例2:
此文参考文章地址:http://stackoverflow.com/questions/4030887/magento-discount-coupan-code-created-on-fly-but-not-working-properly/5894834#5894834
http://blog.leoware.com/magento/coupons-in-magento-part-2.html
http://alanstorm.com/magento_model_generation
原文:www.hellokeykey.com