如何自动生成100美元的优惠券代码?

时间:2016-07-24 06:56:35

标签: php magento2

我需要添加100美元的优惠券代码到简报。我在方法sendConfirmationSuccessEmail中重写了\ Magento \ Newsletter \ Model \ Subscriber

->setTemplateVars(
['subscriber'   => $this, 
 'coupon_code' => $this->getCouponCode()
])

protected function getCouponCode() { . . . . . . return $couponCode; }

如何在magento 2中自动生成优惠券代码?

1 个答案:

答案 0 :(得分:0)

您是否定义了购物车价格规则?如果没有,那就从哪里开始。在管理区域中,转到促销>购物车价格规则>添加新规则,并选择使用自动生成的特定优惠券。

为您要提供的任何特定折扣定义规则后,请保存规则并在购物车价格规则行中记下您刚创建的规则的规则ID。

然后使用您刚创建的规则的规则ID添加如下内容:

下面的函数采用带有以下选项的关联数组:

rule_id =上面的规则ID

qty =指示Magento生成

的优惠券代码数量

length =每个生成的优惠券代码的长度

format = alphanum(对于字母数字代码)或alpha(对于字母代码)或alpha(对于数字代码)

protected function getCouponCode($couponData)
{
    $rule = $this->_loadSalesRule($ruleId);
    // Reference the MassGenerator on this rule.
    /** @var Mage_SalesRule_Model_Coupon_Massgenerator $generator */
    $generator = $rule->getCouponMassGenerator();
    // Validate the generator
    if (!$generator->validateData($couponData)) {
        $this->_critical(Mage::helper('salesrule')->__('Coupon AutoGen API: Invalid parameters passed in.'),
            Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
    } else {
        // Set the data for the generator
        $generator->setData($couponData);
        // Generate a pool of coupon codes for the Generate Coupons rule
        $generator->generatePool();
    }
}