magento用程序生成优惠劵码,上面的代码只能生成Coupon Code,却不能使用,需加入 $coupon->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)->save(); 整合后可以使用的代码如下: // Get the rule id $rule = Mage::getModel('salesrule/rule')->load(1); // Define a coupon code generator model instance // Look at Mage_SalesRule_Model_Coupon_Massgenerator for options $generator = Mage::getModel('salesrule/coupon_massgenerator'); $parameters = array( 'count'=>1, 'format'=>'alphanumeric', 'dash_every_x_characters'=>4, 'prefix'=>'test', 'suffix'=>'test2', 'length'=>12 ); if( !empty($parameters['format']) ){ switch( strtolower($parameters['format']) ){ case 'alphanumeric': case 'alphanum': $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC ); break; case 'alphabetical': case 'alpha': $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL ); break; case 'numeric': case 'num': $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC ); break; } } $generator->setDash( !empty($parameters['dash_every_x_characters'])? (int) $parameters['dash_every_x_characters'] : 0); $generator->setLength( !empty($parameters['length'])? (int) $parameters['length'] : 6); $generator->setPrefix( !empty($parameters['prefix'])? $parameters['prefix'] : ''); $generator->setSuffix( !empty($parameters['suffix'])? $parameters['suffix'] : ''); // Set the generator, and coupon type so it's able to generate $rule->setCouponCodeGenerator($generator); $rule->setCouponType( Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO ); // Get as many coupons as you required $count = !empty($parameters['count'])? (int) $parameters['count'] : 1; $codes = array(); for( $i = 0; $i < $count; $i++ ){ $coupon = $rule->acquireCoupon(); $coupon->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)->save(); $code = $coupon->getCode(); $codes[] = $code; } return $codes; } (责任编辑:好模板) |