创建一个随机Magento的优惠券 [英] Creating a single random Magento coupon

查看:234
本文介绍了创建一个随机Magento的优惠券的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些麻烦。我想要做的是自动在Magento生成一个随机的优惠券code分别有人订阅我们的通讯时间。优惠券是10美金起跳任何事情,都会有一个进出口。订阅两周后的日期。

I'm having some trouble. What I want to do is automatically generate a single random coupon code in Magento each time someone subscribes to our newsletter. The coupon is 10 dollars off anything and will have an exp. date of two weeks after subscription.

所以,我想写一个简单的脚本,跳闸时,订阅我们的通讯的形式提交该会跟Magento的,Magento的要求对于单个随机优惠券code,设置一些基本价格规则(10块钱了什么,每个客户一用,每券一用,到期代两周),然后返回一个随机的优惠券code(例如:WELCOME5​​798),它可以存储在将要传递的变量,沿着W /第一+姓,并通过MailChimp API电子邮件MailChimp。我已经这一切都想通了,除了如何让法师通过一个PHP脚本生成这样一个code,然后返回表示code(即我有我的形式,我知道如何将值传递给MailChimp)。

So, I'm trying to write a simple script that trips when the "subscribe to our newsletter" form is submitted that will talk to Magento, ask Magento for a single random coupon code, set a few basic price rules (10 bucks off anything, one use per customer, one use per coupon, expires two weeks from generation) and then return a random coupon code (ex:WELCOME5798) which can be stored in a variable that will be passed, along w/ first+last name and e-mail to MailChimp via the MailChimp API. I have all this figured out EXCEPT for how to get Mage to generate such a code via a PHP script and then return said code (i.e. I have my form and I know how to pass values to MailChimp).

我是新来的Magento,所以我有一个艰难的时间。我见过法师/ SalesRule /型号/优惠券code和我见过的人解决有些类似的问题,比如这里的一些例子:<一href=\"http://stackoverflow.com/questions/8908347/magento-create-unique-coupon-$c$cs-through-$c$c-and-mail-it-to-the-customer\">Magento - 通过code。创建专属优惠券codeS,并邮寄给客户

I'm new to Magento, so I'm having a tough time. I've seen the code in Mage/SalesRule/Model/Coupon and I've seen some examples of people solving somewhat similar questions, such as here: Magento - Create Unique Coupon Codes through code and mail it to the customer

但我真的很为在那里开始做这项工作,为我自己的目的的损失。可以使用一些帮助/设置直。 :(谢谢乡亲。

But I'm really at a loss for where to start making this work for my own purposes. Could use some help/setting straight. :( Thanks folks.

推荐答案

那么,什么是你的问题?如何为您的要求,优惠券?或者如何在模块它安排的?

So, what is your question? How to generate coupon for your requirements? Or how to arrange it in module?

您可以使用事件newsletter_subscriber_save_after注入您的自定义操作的认购过程。

You can use event newsletter_subscriber_save_after to inject your custom actions to the subscribe process.

下面是根据您的需要优惠券创作的一个例子

Here is an example of coupon creation according to your needs

<?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的优惠券的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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