每次销售自动生成并通过电子邮件发送唯一的凭证代码

时间:2012-12-12 12:44:52

标签: magento magento-1.6

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

有人可以帮助使用Magento 1.6.2的可用代码,还是指向一个可以完成这项工作的插件?

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

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

我在不同的地方找到了代码,每个代码都有一些帮助,但不是一个整体工作的解决方案。最终,我设法将它们一起破解成一个模块 - 这是该模块的观察者.php部分(野兽的关键)。我希望它可以帮助别人!

注意 - 此代码适用于我的场景中的ME。我不能对此提供任何支持,并且对您在申请中的使用不承担任何责任 - 请在您认为合适时使用它。

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

    }

}

0 个答案:

没有答案