创建一个随机的Magento优惠券

3
我遇到了一些问题。我想在Magento中每次有人订阅我们的新闻时自动生成一个随机优惠券码。该优惠券为任意物品提供10美元折扣,有效期为订阅后两周。
因此,我正在尝试编写一个简单的脚本,在“订阅我们的新闻”表单提交时启动,将与Magento通信,请求一个随机的优惠券码,设置几个基本价格规则(任何东西都可以打10美元折扣,每位客户只能使用一次,每张优惠券只能使用一次,从生成开始两周内失效),然后返回一个随机的优惠券码(例如:WELCOME5798),该代码可以存储在变量中,并通过MailChimp API与名字,姓氏和电子邮件一起传递给MailChimp。我已经弄清楚了所有这些,除了如何通过PHP脚本让Mage生成这样的代码并返回该代码(即我有我的表单,也知道如何将值传递给MailChimp)。
我是Magento的新手,所以我很困惑。我看过Mage/SalesRule/Model/Coupon中的代码,并看过一些人解决类似问题的示例,例如在这里:Magento - Create Unique Coupon Codes through code and mail it to the customer 但是我真的不知道从哪里开始让它适用于我的目的。需要一些帮助/指导。:( 谢谢大家。
1个回答

3

那么,您的问题是什么?如何为您的需求生成优惠券?还是如何将其安排在模块中?

您可以使用事件newsletter_subscriber_save_after来将自定义操作注入到订阅过程中。

以下是根据您的需求创建优惠券的示例:

<?php
/**
 * Create coupon for fixed price discount
 *
 * @param int $customer_id
 * @param float $discount
 */
public function createCoupon($customer_id, $discount)
{
    $customer = Mage::getModel('customer/customer')->load($customer_id);

    $customerGroupIds = Mage::getModel('customer/group')->getCollection()->getAllIds();
    $websitesId = Mage::getModel('core/website')->getCollection()->getAllIds();

    $customer_name = $customer->getName();
    $couponCode = Mage::helper('core')->getRandomString(9);

    $model = Mage::getModel('salesrule/rule');
    $model->setName('Discount for ' . $customer_name);
    $model->setDescription('Discount for ' . $customer_name);
    $model->setFromDate(date('Y-m-d'));
    $model->setToDate(date('Y-m-d', strtotime('+2 days')));
    $model->setCouponType(2);
    $model->setCouponCode($couponCode);
    $model->setUsesPerCoupon(1);
    $model->setUsesPerCustomer(1);
    $model->setCustomerGroupIds($customerGroupIds);
    $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('');
    $model->setSortOrder(1);
    $model->setSimpleAction('by_fixed');
    $model->setDiscountAmount($discount);
    $model->setDiscountStep(0);
    $model->setSimpleFreeShipping(0);
    $model->setTimesUsed(0);
    $model->setIsRss(0);
    $model->setWebsiteIds($websitesId);

    try {
        $model->save();
    } catch (Exception $e) {
        Mage::log($e->getMessage());
    }
}

抱歉造成困惑。我想我有点卡住了,因为我们不需要从Magento数据库中收集任何客户信息,因为我们需要的所有客户信息都是通过我们的表单直接发送到MailChimp的。所有新的订阅者都将收到优惠券,因此也不需要指定客户组。由于我对Magento仍然感到非常不舒服,所以我打算尝试快速而简单的方法,实际上编写脚本在Mage之外运行,使用非常有用的require_once('../app/Mage.php');。也许我正在过度复杂化事情。 - Kale
我只是想让Mage创建一个随机优惠券(带有指定的价格规则),并将其发送回以传递给MailChimp API。也许我不能绕过将其添加到Mage模块中(因为我经验不足)。但是感谢你的帮助和耐心,Pavel,我会尝试使用你提供的代码! - Kale
在我的例子中,$customer_id 仅用于生成优惠券描述。您可以将其清空并在 setName() 和 setDescription() 方法中添加自己的描述。再次回到我的代码,您可以在 $couponCode 变量中存储优惠码,因此您可以将其传递给电子邮件模板。 - Pavel Novitsky
Pavel Novitsky,您太棒了。 :) 程式開始變得有意義了!我現在明白了。非常感謝您! - Kale
对于任何遇到编写代码问题的人,我必须添加一行代码来设置存储标签:$model->setStoreLabels("某个标签"); - Tegan Snyder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接