2

我一直在寻找解决这个我试图解决的问题的方法。场景如下:我想为在两个指定日期之间进行的每次购买发放一次性优惠券代码。代金券将在另外两个指定日期之间兑换。代金券必须仅供一次性使用,但如果原始购买者不想要/不需要它,则可能会被赠送。可以为每个客户分配任意数量的个人优惠券代码 - 每次购买都会发出一个。应自动生成优惠券代码并通过电子邮件将其与订单确认一起发送给他们,因此无需手动工作。

有人可以帮助提供 Magento 1.6.2 的可用代码,或者将我指向一个可以完成这项工作的插件吗?

我已经联系 MageStore 询问他们的产品是否符合我的需求,但我想我会在这里问这个问题,以防我能更快地得到答案/解决方案,因为他们目前不可用。

=== 最终工作解决方案 ===

我在不同的地方找到了代码,每个地方都有一些帮助,但对我来说没有一个整体的工作解决方案。最终,我设法将它组合成一个模块 - 这是该模块的observer.php 部分(野兽的症结)。我希望它可以帮助某人!

注意 - 此代码在我的场景中适用于我。我不能对此提供任何支持,也不对它在您的应用程序中的使用承担任何责任 - 请在您认为合适的情况下使用它。

<?php
/**
 * Our class name should follow the directory structure of
 * our Observer.php model, starting from the namespace,
 * replacing directory separators with underscores.
 * i.e. app/code/local/SmashingMagazine/
 *                     LogProductUpdate/Model/Observer.php
 */
class Robgt_AutoVoucher_Model_Observer
{
    /**
     * Magento passes a Varien_Event_Observer object as
     * the first parameter of dispatched events.
     */
    public function autoVoucher(Varien_Event_Observer $observer)
    {

        // Should we run this code at all?
        // Between 17th at midnight and 24th at midday December 2012 ONLY
        $from = strtotime('2012-12-17 00:00:01');
        $to = strtotime('2012-12-24 11:59:59');
        $now = time();

        if($from <= $now && $to >= $now) {

            //Get the Order ID
            $order = $observer->getEvent()->getOrder();
            $orderEntityId = $order->getId(); 
            $orderid = $order->getIncrementId();
            //$orderid = Mage::getSingleton('checkout/session')->getLastRealOrderId();

            if(empty($orderid))
            {
                $orderid = '999999999';
            }

            // Grab the last 3 digits of the Order number for use in the voucher code
            $last_orderNumberDigits = substr($orderid, -3, 3);; 

            // Create coupon code random part
            $codeLength = 7;
            $chars = "ABCDEFGHIJKLMNPQRSTUVWXYZ1234567890";
            srand((double)microtime()*1000000);
            $i = 0;
            $code = '' ;
            while ($i <= $codeLength)
            {
                $num = rand() % 33;
                $tmp = substr($chars, $num, 1);
                $code = $code . $tmp;
                $i++;
            }

            $couponRandomCode = $code;

            // Build coupon code
            // Format is: MH[Last 3 Digits of Order number]-[5 Random digits]
            $couponCode = 'MH'.$last_orderNumberDigits.'-'.$couponRandomCode;
            $couponName = 'Xmas2012 - '.$couponCode;

            // Build the coupon rule
            $rule = Mage::getModel('salesrule/rule');

            $rule->setName($couponName);
            $rule->setDescription('Xmas 2012 Discount coupon');
            $rule->setFromDate('2013-01-01'); // Jan 1st
            $rule->setToDate('2013-01-31'); // Jan 31st
            $rule->setCouponType(2);
            $rule->setCouponCode($couponCode);
            $rule->setUsesPerCoupon(1);
            $rule->setUsesPerCustomer(1);
            $rule->setCustomerGroupIds('0,1,3,4');
            $rule->setIsActive(1);

            $conditions = array(
                "1" => array(
                    'type' => 'salesrule/rule_condition_combine',
                    'aggregator' => 'all',
                    'value' => 1,
                    'new_child' => false
                    ),
                "1--1" => array(
                    'type' => 'salesrule/rule_condition_product_found',
                    'value' => 1,
                    'aggregator' => 'all',
                    'new_child' => false
                ),
                "1--1--1" => array(
                    'type' => 'salesrule/rule_condition_product',
                    'attribute' => 'category_ids',
                    'operator' => '!()',
                    'value' => '932,341,800,1116'
                )
            );
            $actions = array(
                "1" => array(
                        "type"          => "salesrule/rule_condition_product",
                        "aggregator"    => "all",
                        "value"         => "1",
                        "new_child"     => false
                ),
                "1--1" => array(
                        "type"          => "salesrule/rule_condition_product",
                        "attribute"     => "category_ids",
                        'operator' => '!()',
                        'value' => '932,341,800,1116'
                )
            );

            $rule->setData('conditions',$conditions);
            $rule->setData("actions",$actions);
            $rule->setStopRulesProcessing(1);
            $rule->setIsAdvanced(1);
            $rule->setProductIds('');
            $rule->setSortOrder(0);
            $rule->setSimpleAction('by_percent');
            $rule->setDiscountAmount(10);
            $rule->setDiscountStep(0);
            $rule->setSimpleFreeShipping(0);
            $rule->setTimesUsed(0);
            $rule->setIsRss(0);
            $rule->setWebsiteIds('1');

            $rule->loadPost($rule->getData());
            $rule->save();


            // Email the details to Trev
            $to  = 'trevor.wallinger@gmail.com';
            //$to  = 'robgt.com@gmail.com';
            $subject = 'New Xmas 2012 Voucher Code';
            $message = '<body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
            <div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
            <table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%">
                <tr>
                    <td align="center" valign="top" style="padding:20px 0 20px 0">
                        <!-- [ header starts here] -->
                        <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                            <tr>
                                <td valign="top">
                                    <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;">A new coupon code was generated, as follows:</h1>
                                    <p style="font-size:12px; line-height:16px; margin:0 0 8px 0;"><strong>Order Number:</strong> '.$orderid.'</p>
                                    <p style="font-size:12px; line-height:16px; margin:0 0 8px 0;"><strong>Coupon Code:</strong> '.$couponCode.'</p>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            </div>
            </body>     
            ';
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";     

            try {
                mail($to, $subject, $message, $headers);
                $mailReport = 'Mail sent!';
            }
            catch(Exception $ex) {
                $mailReport = "Mail failed to send!";
            }

            // Log the voucher code generated
            // Saved to /var/log/coupons.log
            Mage::log(
                "Coupon created: {$couponCode} | Order ID: {$orderid} | Mail Report: {$mailReport}\n\r",
                null, 
                'coupons.log'
            );

        } // End If Date check.

    }

}
4

0 回答 0