在 Magento 中以编程方式创建自动生成的优惠券代码? [英] Programmatically create auto generated coupon codes in Magento?

查看:22
本文介绍了在 Magento 中以编程方式创建自动生成的优惠券代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于其他 stackoverflow 帖子,我已经能够使用以下代码在 Magento 中以编程方式生成个人购物车价格规则优惠券.

Based on other stackoverflow posts, I've been able to use the following code to programmatically generate individual shopping cart price rule coupons in Magento.

如何以编程方式调用自动生成优惠券"功能,为我制定的每个定价规则创建 100 张独特的优惠券?谢谢!

How can I programmatically call the "Auto Generate Coupon" feature to create 100 unique coupons for each price rule I make? Thanks!

$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 等).

For instance, this one price rule might have a whole list of Auto-Generated Coupon Codes (htgf-7774, htgf-2345, etc) using the function that is available when manually creating price rules in the admin panel.

我已经接近了,使用以下代码.还是不知道如何具体指定自动生成模式

I've gotten closer, using the following code. Still don't know how to specifically assign the auto generation pattern

->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)

推荐答案

感谢我在谷歌搜索时发现的一个漂亮的帖子 (http://fragmentedthought.com/fragments/programatically-creating-sales-rule-coupon-code),我回答了我自己的问题:

Thanks to a nifty post I found while googling this (http://fragmentedthought.com/fragments/programatically-creating-sales-rule-coupon-code), I answered my own question:

// 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;

这成功生成了以下代码:

This successfully generated the following 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

这篇关于在 Magento 中以编程方式创建自动生成的优惠券代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆