2

私は解決しようとしているこの問題の解決策を探し回っています。シナリオは次のとおりです。指定された 2 つの日付の間に行われたすべての購入に対して、使い捨てのバウチャー コードを発行したいと考えています。バウチャーは、別の 2 つの指定された日付の間に引き換えられます。バウチャーは 1 回のみ使用する必要がありますが、元の購入者がそれを望まない/必要としない場合は、譲渡される可能性があります。各顧客には、購入ごとに発行される個別のバウチャー コードをいくつでも割り当てることができます。バウチャー コードは自動的に生成され、注文確認とともに電子メールで送信されるため、手作業は必要ありません。

誰かが Magento 1.6.2 の使用可能なコードを手伝ってくれますか、それとも仕事をするプラグインを教えてくれますか?

MageStore に連絡して、ニーズに合っているかどうかを尋ねましたが、現時点では利用できないため、回答/解決策をより迅速に得ることができる場合は、ここで質問すると思いました.

=== 最終的な作業ソリューション ===

さまざまな場所でコードを見つけましたが、それぞれに役立つビットがありましたが、全体的に機能するソリューションは 1 つではありませんでした。最終的に、私はこれをモジュールにハックすることに成功しました - これがそのモジュールの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