magento 2 generate coupon code based on a rule programmaticallyMagento free shipping coupon breaking other coupons (and itself)Create Magento Coupon via APICreating a single random Magento couponDeleting a large number of Magento coupons (shopping cart price rules) programaticallyProgrammatically create auto generated coupon codes in Magento?Couldnot create a coupon code programmaticallyMagento: Stop coupon code automatically applyingMagento Promotion Rule Coupon Code is not ValidMagento Coupon code is getting applied but not showing reduced amount in totalHow we get welcome email with coupon code

Sum and average calculator

German equivalent to "going down the rabbit hole"

How do I get my neighbour to stop disturbing with loud music?

Quick Tilepaint Puzzles: Corridors and Corners

Using font to highlight a god's speech in dialogue

Are spot colors limited and why CMYK mix is not treated same as spot color mix?

How to save money by shopping at a variety of grocery stores?

How does the search space affect the speed of an ILP solver?

Why haven't the British protested Brexit as ardently as the Hong Kong protesters?

Can you use Apple Care+ without any checks (bringing just MacBook)?

Tikz: Draw simplified BLE-Stack

In what language did Túrin converse with Mím?

Calculate Landau's function

Break down the phrase "shitsurei shinakereba naranaindesu"

The correct way of compute indicator function in Mathematica

Heuristic argument for the Riemann Hypothesis

How to Flip Rotation from Positive to Negative?

Can two aircraft be allowed to stay on the same runway at the same time?

How can I portray a character with no fear of death, without them sounding utterly bored?

How were US credit cards verified in-store in the 1980's?

Which is the correct version of Mussorgsky's Pictures at an Exhibition?

What's the origin of the concept of alternate dimensions/realities?

Why doesn't Starship have four landing legs?

I failed to respond to a potential advisor



magento 2 generate coupon code based on a rule programmatically


Magento free shipping coupon breaking other coupons (and itself)Create Magento Coupon via APICreating a single random Magento couponDeleting a large number of Magento coupons (shopping cart price rules) programaticallyProgrammatically create auto generated coupon codes in Magento?Couldnot create a coupon code programmaticallyMagento: Stop coupon code automatically applyingMagento Promotion Rule Coupon Code is not ValidMagento Coupon code is getting applied but not showing reduced amount in totalHow we get welcome email with coupon code






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a rule created in Magento 2, e.g. 10% OFF, this rule uses auto generated coupon code, instead of a pre-assigned code.



I'd like to know how I can generate codes based on the rule programmatically.



NOTE: This is not about creating a new rule programmatically. I want to generate codes based on a existing rule, so I can add more codes when needed but doesn't have to create a new rule every time, e.g. when a customer subscribes our mailing list, I can execute the code to generate a new code under the same rule and send it to the subscriber.



I found this post, but this is about creating a new rule;



https://magento.stackexchange.com/questions/207311/magento2-2-programmatically-create-coupon



I found the solution for Magento 1:



https://mage2-blog.com/magento-1-programmatically-create-promotion-coupon-codes/



Thanks










share|improve this question
























  • Right now you've kind of given us a spec, some possibly related code, and asked us to do your coding - and that's not how this site works. You need to digest the links you've provided and then present us with the code that you want to try, letting us know what is and isn't working and how you want it to work. We can then help you fix your code.

    – Enigmativity
    Mar 27 at 23:35

















0















I have a rule created in Magento 2, e.g. 10% OFF, this rule uses auto generated coupon code, instead of a pre-assigned code.



I'd like to know how I can generate codes based on the rule programmatically.



NOTE: This is not about creating a new rule programmatically. I want to generate codes based on a existing rule, so I can add more codes when needed but doesn't have to create a new rule every time, e.g. when a customer subscribes our mailing list, I can execute the code to generate a new code under the same rule and send it to the subscriber.



I found this post, but this is about creating a new rule;



https://magento.stackexchange.com/questions/207311/magento2-2-programmatically-create-coupon



I found the solution for Magento 1:



https://mage2-blog.com/magento-1-programmatically-create-promotion-coupon-codes/



Thanks










share|improve this question
























  • Right now you've kind of given us a spec, some possibly related code, and asked us to do your coding - and that's not how this site works. You need to digest the links you've provided and then present us with the code that you want to try, letting us know what is and isn't working and how you want it to work. We can then help you fix your code.

    – Enigmativity
    Mar 27 at 23:35













0












0








0








I have a rule created in Magento 2, e.g. 10% OFF, this rule uses auto generated coupon code, instead of a pre-assigned code.



I'd like to know how I can generate codes based on the rule programmatically.



NOTE: This is not about creating a new rule programmatically. I want to generate codes based on a existing rule, so I can add more codes when needed but doesn't have to create a new rule every time, e.g. when a customer subscribes our mailing list, I can execute the code to generate a new code under the same rule and send it to the subscriber.



I found this post, but this is about creating a new rule;



https://magento.stackexchange.com/questions/207311/magento2-2-programmatically-create-coupon



I found the solution for Magento 1:



https://mage2-blog.com/magento-1-programmatically-create-promotion-coupon-codes/



Thanks










share|improve this question














I have a rule created in Magento 2, e.g. 10% OFF, this rule uses auto generated coupon code, instead of a pre-assigned code.



I'd like to know how I can generate codes based on the rule programmatically.



NOTE: This is not about creating a new rule programmatically. I want to generate codes based on a existing rule, so I can add more codes when needed but doesn't have to create a new rule every time, e.g. when a customer subscribes our mailing list, I can execute the code to generate a new code under the same rule and send it to the subscriber.



I found this post, but this is about creating a new rule;



https://magento.stackexchange.com/questions/207311/magento2-2-programmatically-create-coupon



I found the solution for Magento 1:



https://mage2-blog.com/magento-1-programmatically-create-promotion-coupon-codes/



Thanks







magento magento2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 23:20









JerryJerry

162 bronze badges




162 bronze badges















  • Right now you've kind of given us a spec, some possibly related code, and asked us to do your coding - and that's not how this site works. You need to digest the links you've provided and then present us with the code that you want to try, letting us know what is and isn't working and how you want it to work. We can then help you fix your code.

    – Enigmativity
    Mar 27 at 23:35

















  • Right now you've kind of given us a spec, some possibly related code, and asked us to do your coding - and that's not how this site works. You need to digest the links you've provided and then present us with the code that you want to try, letting us know what is and isn't working and how you want it to work. We can then help you fix your code.

    – Enigmativity
    Mar 27 at 23:35
















Right now you've kind of given us a spec, some possibly related code, and asked us to do your coding - and that's not how this site works. You need to digest the links you've provided and then present us with the code that you want to try, letting us know what is and isn't working and how you want it to work. We can then help you fix your code.

– Enigmativity
Mar 27 at 23:35





Right now you've kind of given us a spec, some possibly related code, and asked us to do your coding - and that's not how this site works. You need to digest the links you've provided and then present us with the code that you want to try, letting us know what is and isn't working and how you want it to work. We can then help you fix your code.

– Enigmativity
Mar 27 at 23:35












1 Answer
1






active

oldest

votes


















0















It is actually easier than I thought. Code below worked for me:



$couponGenerator = $objectManager->create('MagentoSalesRuleModelCouponGenerator');

$data = array(
'rule_id' => $rule->getId(),
'qty' => '1',
'length' => '12',
'format' => 'alphanum',
'prefix' => 'pre',
'suffix' => 'suf',
);

$codes = $couponGenerator->generateCodes($data);





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387944%2fmagento-2-generate-coupon-code-based-on-a-rule-programmatically%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    It is actually easier than I thought. Code below worked for me:



    $couponGenerator = $objectManager->create('MagentoSalesRuleModelCouponGenerator');

    $data = array(
    'rule_id' => $rule->getId(),
    'qty' => '1',
    'length' => '12',
    'format' => 'alphanum',
    'prefix' => 'pre',
    'suffix' => 'suf',
    );

    $codes = $couponGenerator->generateCodes($data);





    share|improve this answer





























      0















      It is actually easier than I thought. Code below worked for me:



      $couponGenerator = $objectManager->create('MagentoSalesRuleModelCouponGenerator');

      $data = array(
      'rule_id' => $rule->getId(),
      'qty' => '1',
      'length' => '12',
      'format' => 'alphanum',
      'prefix' => 'pre',
      'suffix' => 'suf',
      );

      $codes = $couponGenerator->generateCodes($data);





      share|improve this answer



























        0














        0










        0









        It is actually easier than I thought. Code below worked for me:



        $couponGenerator = $objectManager->create('MagentoSalesRuleModelCouponGenerator');

        $data = array(
        'rule_id' => $rule->getId(),
        'qty' => '1',
        'length' => '12',
        'format' => 'alphanum',
        'prefix' => 'pre',
        'suffix' => 'suf',
        );

        $codes = $couponGenerator->generateCodes($data);





        share|improve this answer













        It is actually easier than I thought. Code below worked for me:



        $couponGenerator = $objectManager->create('MagentoSalesRuleModelCouponGenerator');

        $data = array(
        'rule_id' => $rule->getId(),
        'qty' => '1',
        'length' => '12',
        'format' => 'alphanum',
        'prefix' => 'pre',
        'suffix' => 'suf',
        );

        $codes = $couponGenerator->generateCodes($data);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 29 at 22:15









        JerryJerry

        162 bronze badges




        162 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387944%2fmagento-2-generate-coupon-code-based-on-a-rule-programmatically%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현