创建一个随机的Magento优惠券

时间:2012-10-09 19:20:43

标签: php magento automation auto-generate coupon

我遇到了麻烦。我想要做的是每当有人订阅我们的时事通讯时,就会在Magento中自动生成一个随机优惠券代码。优惠券是任何东西10美元,将有一个exp。订阅后两周的日期。

所以,我试图编写一个简单的脚本,当"订阅我们的时事通讯"表格提交将与Magento交谈,向Magento索取一个随机优惠券代码,设置一些基本的价格规则(任何10块钱,每位客户一次使用,每张优惠券一次使用,从发电后两周到期)然后返回随机优惠券代码(例如:WELCOME5​​798),可以存储在一个将传递的变量中,带有第一个+姓氏,并通过MailChimp API发送给MailChimp。除了如何让Mage通过PHP脚本生成这样的代码然后返回所述代码(即我有我的表单,我知道如何将值传递给MailChimp)之外,我已经解决了所有这些问题。

我是Magento的新手,所以我度过了一段艰难的时光。我已经看过Mage / SalesRule / Model / Coupon中的代码,我看过一些人解决类似问题的例子,例如:Magento - Create Unique Coupon Codes through code and mail it to the customer

但是我真的不知道从哪里开始为自己的目的开展这项工作。可以直接使用一些帮助/设置。 :(谢谢大家。

1 个答案:

答案 0 :(得分: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());
    }
}
相关问题