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

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript