• magento 用程序生成优惠劵码


    参考自http://fragmentedthought.com/fragments/programatically-creating-sales-rule-coupon-code

    上面的代码只能生成Coupon Code,却不能使用,需加入

    $coupon->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERAT‌​ED)->save();

    整合后可以使用的代码如下:

     // Get the rule id
            $rule = Mage::getModel('salesrule/rule')->load(1);
    
            // Define a coupon code generator model instance
            // Look at Mage_SalesRule_Model_Coupon_Massgenerator for options
            $generator = Mage::getModel('salesrule/coupon_massgenerator');
    
            $parameters = array(
                'count'=>1,
                'format'=>'alphanumeric',
                'dash_every_x_characters'=>4,
                'prefix'=>'test',
                'suffix'=>'test2',
                'length'=>12
            );
    
            if( !empty($parameters['format']) ){
                switch( strtolower($parameters['format']) ){
                    case 'alphanumeric':
                    case 'alphanum':
                        $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC );
                        break;
                    case 'alphabetical':
                    case 'alpha':
                        $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL );
                        break;
                    case 'numeric':
                    case 'num':
                        $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC );
                        break;
                }
            }
    
            $generator->setDash( !empty($parameters['dash_every_x_characters'])? (int) $parameters['dash_every_x_characters'] : 0);
            $generator->setLength( !empty($parameters['length'])? (int) $parameters['length'] : 6);
            $generator->setPrefix( !empty($parameters['prefix'])? $parameters['prefix'] : '');
            $generator->setSuffix( !empty($parameters['suffix'])? $parameters['suffix'] : '');
    
            // Set the generator, and coupon type so it's able to generate
            $rule->setCouponCodeGenerator($generator);
            $rule->setCouponType( Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO );
    
            // Get as many coupons as you required
            $count = !empty($parameters['count'])? (int) $parameters['count'] : 1;
            $codes = array();
            for( $i = 0; $i < $count; $i++ ){
                $coupon = $rule->acquireCoupon();
                $coupon->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)->save();
                $code = $coupon->getCode();
                $codes[] = $code;
            }
            return $codes;
        }
  • 相关阅读:
    luoguP2016 战略游戏
    [Usaco2006 Nov]Corn Fields牧场的安排
    [Ahoi2009]self 同类分布
    POJ3208:Apocalypse Someday
    [usaco2010 Oct]Soda Machine
    [Usaco2005 Dec]Scales 天平
    PTA的Python练习题(十九)
    堆叠注入
    PHP序列化与反序列化(三)总结实战
    攻防世界web进阶1-12总结篇
  • 原文地址:https://www.cnblogs.com/cangzhou/p/3756992.html
Copyright © 2020-2023  润新知