编程创建在Magento自动生成优惠券代码?(Programmatically create aut

2019-07-21 02:59发布

基于其它计算器职位,我已经能够使用下面的代码以编程方式生成在Magento个人的购物车价格规则的优惠券。

我如何编程方式调用“自动生成优惠券”功能,以便为每个标价规定我做创造100张独特的优惠券? 谢谢!

$coupon = Mage::getModel('salesrule/rule');
$coupon->setName($_coupon['name'])
       ->setDescription('this is a description')
       ->setFromDate(date('Y-m-d'))
       ->setCouponType(2)
       ->setCouponCode($_coupon['code'])
       ->setUsesPerCoupon(1000)
       ->setUsesPerCustomer(100)
       ->setCustomerGroupIds(array(1)) //an array of customer groupids
       ->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(100)
       ->setDiscountQty(null)
       ->setDiscountStep('0')
       ->setSimpleFreeShipping('0')
       ->setApplyToShipping('0')
       ->setIsRss(0)
       ->setWebsiteIds(array(1));      
$coupon->save();

举例来说,这一价格规则可能有使用可在管理面板手动创建的价格规则时功能自动生成优惠券代码(htgf-7774,htgf-2345等)的完整列表。

编辑:

我已经得到接近,使用下面的代码。 还是不知道如何具体分配自动生成模式

->setName('Name')
->setDescription('this is a description')
->setFromDate('2013-03-06')
->setToDate(NULL)
->setUsesPerCustomer('100')
->setIsActive('1')
->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(NULL)
->setSortOrder('0')
->setSimpleAction('by_percent')
->setDiscountAmount('100.0000')
->setDiscountQty(NULL)
->setDiscountStep('0')
->setSimpleFreeShipping('0')
->setApplyToShipping('0')
->setTimesUsed('1')
->setIsRss('0')
->setCouponType('2')
->setUseAutoGeneration('1')
->setUsesPerCoupon('1000')
->setCustomerGroupIds(array('1',))
->setWebsiteIds(array('1',))
->setCouponCode(NULL)

Answer 1:

多亏了一记漂亮的文章,我发现谷歌搜索,而这( http://fragmentedthought.com/fragments/programatically-creating-sales-rule-coupon-code ),我回答我自己的问题:

// Get the rule in question
$rule = Mage::getModel('salesrule/rule')->load(21); //21 = ID of coupon in question

// 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'=>5,
    'format'=>'alphanumeric',
    'dash_every_x_characters'=>4,
    'prefix'=>'ABCD-EFGH-',
    'suffix'=>'-WXYZ',
    'length'=>8
);

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();
  $code = $coupon->getCode();
  $codes[] = $code;
}
return $codes;

这成功地生成以下代码:

ABCD-EFGH-ZC6V-ZJWD-WXYZ
ABCD-EFGH-4XMX-353L-WXYZ
ABCD-EFGH-XCJB-5GQI-WXYZ
ABCD-EFGH-UEAO-L1NJ-WXYZ
ABCD-EFGH-59B3-50T2-WXYZ


文章来源: Programmatically create auto generated coupon codes in Magento?