Random Alphanumeric Coupon Code Generator

I want to generate alphanumeric coupons for my woo commerece store, including special characters.

is there a way to do it, using the functions and tools available in make.com

Hi @Shashwat_Chopra,

A simple way to generate coupon codes would be to convert unique id to Base 64. Like this:

This example above will give you 16-character code including uppercase (A-Z), lowercase (a-z) and numbers (0-9), but not all special characters, only β€œ+” and β€œ/” symbols. Like this:

image

It would be possible to get exactly what you want including special characters using a Repeater module (from Flow Control) and either Set Variables or Switch modules… but this would use a lot of operations. Is that something you’d like to try?

2 Likes

Interesting question, I generated a 6-character string for your cupon by doing the following:

1-. create an array from a string delimited by . like this using the split fucntion : {{split(β€œA.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z.0.1.2.3.4.5.6.7.8.9.!.@.#.$.%.^.&.*.β€β€œβ€; β€œ.”)}}

2-. And then create six variables, getting a random number for the position of the array
3- Finally put all array values in a single variable.

This will consume only 3 operations and you can store the resulting Cupon on a DB to avoid duplicates.

4 Likes

Hi @Rafael_Sanchez,

That is a really good approach! :+1:

While answering this, I built a scenario using Repeater and Aggregator, and it used 18 Operations to generate a 16-character code. Your solution is much, much better than that. :+1:

3 Likes

Generate your coupon code in a single step :+1:

Notes:

  • I used ABCDEFGHJKMNPQRSTUVWXYZ23456789!@#$%^& repeated 4 times to allow for repeated characters in the code, and to make it way less likely that a duplicate is generated. You can have more times but there are diminishing returns

  • I removed ambiguous characters 1ILO0

  • I used a triple shuffle - you could add more shuffles to randomise the array more but there are diminishing returns

  • I used substring 0;6, but you can change the 6 to however long you want your output to be

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV to paste in the canvas.

  3. Click on each imported module and save it. You may need to remap some variables.

JSON

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 1,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "Coupon",
                        "scope": "roundtrip",
                        "value": "{{substring(join(shuffle(shuffle(shuffle(split(\"ABCDEFGHJKMNPQRSTUVWXYZ23456789!@#$%^&ABCDEFGHJKMNPQRSTUVWXYZ23456789!@#$%^&ABCDEFGHJKMNPQRSTUVWXYZ23456789!@#$%^&ABCDEFGHJKMNPQRSTUVWXYZ23456789!@#$%^&\"; emptystring)))); emptystring); 0; 6)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 0,
                            "y": 0,
                            "name": "Generate random code"
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            },
                            {
                                "name": "value",
                                "type": "any",
                                "label": "Variable value"
                            }
                        ],
                        "interface": [
                            {
                                "name": "Coupon",
                                "type": "any",
                                "label": "Coupon"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

Blueprint

blueprint.json (2.8 KB)

11 Likes

thank you everyone. loved the replies

Elegant, nice solution @samliew