Question

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.

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

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.

Était-ce utile?

La solution

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

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());
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top