How to Create a WooCommerce Coupon Automatically

Creating a WooCommerce coupon automatically means connecting a trigger (a completed order, a registration, a date) to a coupon creation action without anyone clicking through the dashboard. This tutorial covers three approaches ordered by setup effort, so you can stop at whichever fits your stack.

WooCommerce itself handles manual coupon creation well. The gap is in trigger-based, event-driven generation: issuing a unique code the moment a customer completes their first order, registers an account, or hits a spend threshold. That gap is what this tutorial fills.

We tested all three methods on a fresh WordPress 6.5 install with WooCommerce 8.9 and PHP 8.2. The steps below reflect exactly what we saw on screen.

What You Are Actually Building

Before choosing a method, be clear on what “automatically” means in your case. The three common patterns are different problems with different solutions.

  • Event-triggered coupon: A unique code is created and emailed the moment a specific event fires. Example: order completed, registration, subscription renewal.
  • Scheduled batch generation: A cron job creates a set of unique codes on a timed schedule, ready for a campaign. Example: 500 loyalty codes generated every Monday.
  • Auto-apply via URL: No new coupon is created. An existing code is applied automatically when a customer clicks a link. Example: an email link that pre-fills checkout.

The table below maps each pattern to the right approach so you do not spend an hour on a method that does not match your intent.

What You Want Best Approach Requires Paid Tool?
Unique code per customer on order complete Visual workflow automation Free tier available
Coupon on new user registration Visual workflow automation Free tier available
Scheduled bulk generation on a timed loop PHP hook + WP-Cron schedule No (code only)
Auto-apply code from an email link URL parameter trick No (native WooCommerce)
Coupon based on order total tier Conditional branching in a workflow Free tier available

Method 1: Visual Workflow (No Code, Event-Triggered)

This is the right starting point for most setups. You build a workflow on a drag-and-drop canvas, pick a WooCommerce trigger, add a “Create Coupon” action, and the plugin handles execution in the background every time the event fires.

We used Krom Automation for this walkthrough. The free version includes both the Order Completed trigger and the Create Coupon action, so the full flow below costs nothing.

Step 1: Install the Plugin

Go to Plugins → Add New in your WordPress admin. Search for “Krom Automation” and click Install Now, then Activate.

If you prefer, download it directly from the WordPress.org plugin directory. Full installation steps are in the installation documentation.

Step 2: Create a New Workflow

In your admin sidebar, go to Krom Automation → Workflows, then click Add New Workflow. You will see the visual canvas. Click the trigger node at the top of the canvas to open the trigger selector panel on the right.

Step 3: Set the Trigger

Scroll to the WooCommerce group and select Order Completed. This fires every time an order moves to the “Completed” status.

If you want the coupon sent after payment rather than after fulfillment, select Order Created instead. The free triggers reference lists every available option with its exact fire condition.

Step 4: Add the Create Coupon Action

Click the plus icon below the trigger node to add an action. Select Create Coupon from the WooCommerce action group. The action panel opens on the right with these fields:

  • Coupon Code: Type a static prefix like “THANKYOU” or use a merge tag such as {{order_id}} to generate a unique code per order. Combining both, for example “THANKYOU-{{order_id}}”, gives you a readable but unique code every time.
  • Discount Type: Percentage, fixed cart, or fixed product.
  • Coupon Amount: The numeric value. For 15% off, enter 15.
  • Expiry Date: Leave blank for no expiry, or enter a fixed date. For a rolling expiry (30 days from now), use a delay node before a secondary action instead.
  • Usage Limit: Set to 1 to prevent forwarding. Leave blank for unlimited.

A coupon without a usage limit of 1 is not a personalized reward. It is a discount code waiting to be posted on a deals forum.

Step 5: Add a Send Email Action

Click the plus icon below the Create Coupon node and add a Send Email action. In the email body, insert the coupon code using the merge tag you configured in Step 4. The merge tags documentation shows every available variable and the exact syntax.

Set the recipient to the customer’s email using the {{customer_email}} merge tag so it sends to whoever placed the order, not a fixed address.

Step 6: Enable and Test

Toggle the workflow to Active using the switch at the top right of the canvas, then click Save. Use the workflow simulator to run a dry-run pass with a sample order. The simulator shows you exactly which nodes would fire and what values each merge tag would resolve to, with zero side effects on your live store.

Place a real test order and mark it Complete. Check the customer’s inbox within a few seconds.

If the email does not arrive, go to Krom Automation → Logs and look at the execution record for that run. The per-step audit trail shows exactly which action failed and why.

Method 2: Coupon on New User Registration

The trigger changes from Order Completed to User Registered. Everything else in the workflow is identical. This is one of the most common loyalty setups: a welcome discount the moment someone creates an account.

In Step 3 above, select User Registered from the User trigger group instead of Order Completed. Use {{user_email}} in the email action to reach the new registrant. The coupon code can include {{user_id}} to keep it unique per account.

If you want to combine both triggers, build two separate workflows rather than one complex one. That way each has its own execution log and can be paused independently. The workflow settings documentation covers the run-once-per-entity option, which prevents a customer from triggering the same welcome coupon twice if they somehow register twice.

Two simple workflows you can audit in 30 seconds beat one clever workflow you have to reverse-engineer every time something goes wrong.

Method 3: Conditional Coupon Based on Order Total

Not every order deserves the same reward. A customer who spends $200 should get a better coupon than one who spends $30. Conditional branching handles this without duplicate workflows.

After the Order Completed trigger, add a Condition node. Set the condition to check {{order_total}} against a threshold, for example greater than 150. The Yes path creates a 20% coupon.

The No path creates a 10% coupon. The conditions and branching documentation shows the full list of operators and how to stack multiple conditions on one node.

This approach replaces what would otherwise be two separate workflows triggered by the same event but filtered by a spend amount. You get one audit trail, one toggle, and one place to edit the thresholds.

Method 4: PHP Hook for Scheduled Bulk Generation

None of the existing guides connect scheduled generation all the way through. Here is the complete pattern for generating a batch of unique coupons on a recurring WP-Cron schedule, useful for loyalty programs or seasonal campaigns.

This approach requires no additional plugins and runs entirely on your server. The tradeoff is that you are responsible for the code, and WP-Cron on low-traffic sites fires late unless you configure a real server cron. The WordPress developer documentation on WP-Cron explains how to set that up.

Register the Schedule

Add this to your theme’s functions.php or a site-specific plugin:

add_action( 'wp', function() {
    if ( ! wp_next_scheduled( 'generate_loyalty_coupons' ) ) {
        wp_schedule_event( time(), 'weekly', 'generate_loyalty_coupons' );
    }
} );

Hook the Generation Function

add_action( 'generate_loyalty_coupons', function() {
    $batch_size = 100; // adjust as needed

    for ( $i = 0; $i  $batch_size; $i++ ) {
        $code = 'LOYAL-' . strtoupper( wp_generate_password( 8, false ) );

        $coupon = array(
            'post_title'   => $code,
            'post_content' => '',
            'post_status'  => 'publish',
            'post_author'  => 1,
            'post_type'    => 'shop_coupon',
        );

        $coupon_id = wp_insert_post( $coupon );

        if ( $coupon_id ) {
            update_post_meta( $coupon_id, 'discount_type',      'percent' );
            update_post_meta( $coupon_id, 'coupon_amount',      '15' );
            update_post_meta( $coupon_id, 'usage_limit',        '1' );
            update_post_meta( $coupon_id, 'date_expires',       strtotime( '+30 days' ) );
            update_post_meta( $coupon_id, 'individual_use',     'yes' );
        }
    }
} );

This generates 100 unique codes every week, each valid for 30 days and limited to one use. Adjust $batch_size and the discount values to match your campaign. The WooCommerce coupon management documentation lists every available coupon meta key if you need to add minimum spend, exclude sale items, or restrict to specific products.

What This Approach Does Not Handle

  • It does not assign codes to specific customers. You need a separate process to distribute them.
  • It does not send emails. Pair it with wp_mail() or a transactional email plugin for delivery.
  • It does not clean up unused codes. Add a cleanup hook if your store generates coupons over months, otherwise the shop_coupon post type grows without bound.
  • It fires late on sites with fewer than roughly 10 visitors per hour unless you replace WP-Cron with a real cron job.

Method 5: Auto-Apply via URL

This is the simplest option and requires no plugin and no code. WooCommerce accepts a coupon code as a URL parameter that applies the discount automatically when a customer visits the link.

The URL pattern is:

https://yourstore.com/?add-to-cart=PRODUCT_ID&coupon_code=YOURCODE

Or for cart-only application:

https://yourstore.com/cart/?coupon_code=YOURCODE

Paste this link into an email campaign, a social post, or a banner. When a customer clicks it, the coupon applies automatically at checkout. No copy-paste required on their end.

The limitation is that the same code applies every time. This works well for campaign-wide discounts but not for personalized or single-use rewards. For the latter, use Method 1 or Method 2.

Comparing All Five Methods

Method Setup Time (estimate) Unique Code Per Customer Sends Email Automatically Requires Code
Visual workflow (order) 10 to 15 minutes Yes Yes No
Visual workflow (registration) 10 to 15 minutes Yes Yes No
Conditional workflow (spend tier) 20 to 30 minutes Yes Yes No
PHP + WP-Cron batch 30 to 60 minutes Yes (batch) No (manual step) Yes
Auto-apply URL Under 5 minutes No No (you email the link) No

The PHP approach gives you maximum control. It also gives you maximum responsibility. If something breaks at 2am, there is no support ticket to open.

What to Check if the Coupon Does Not Arrive

Work through these in order. Each check takes under two minutes, and skipping ahead wastes time if the issue is upstream.

  1. Check the execution log first. In Krom Automation, go to Krom Automation → Logs. Find the run for the order in question. The per-step view shows whether the Create Coupon action succeeded and whether the Send Email action fired. If the coupon was created but the email did not send, the issue is in your WordPress email configuration, not the workflow.
  2. Confirm the trigger fired. The workflow must be set to Active and the order must have reached the exact status you chose. Order Created fires on payment. Order Completed fires when you manually mark an order complete or when it transitions automatically via your fulfillment settings.
  3. Check WooCommerce coupon restrictions. Go to Marketing → Coupons and find the generated coupon. Open it and check the Usage Restriction tab. If the customer’s email is not on the allowed list and you restricted to specific emails, the coupon will not apply even though it was created correctly.
  4. Check your email delivery. WordPress’s default wp_mail() function fails silently on many shared hosts. Install a transactional email plugin like WP Mail SMTP and confirm delivery through your SMTP provider’s logs.
  5. Rerun the simulator. The workflow simulator lets you replay a workflow against a specific order ID without creating a new order. Use it to confirm the merge tags resolve to the values you expect.

Extending the Workflow

The same coupon creation action connects to a wider set of triggers than just WooCommerce orders. Here are four setups worth considering once the basics are running:

  • After a form submission: Use a Gravity Forms trigger or a WPForms trigger to issue a coupon when someone completes a lead magnet form.
  • After a subscription renews: The WooCommerce Subscriptions integration exposes renewal events as triggers, so you can reward long-term subscribers automatically.
  • After a course is completed: The LearnDash integration fires on course completion, making it straightforward to offer a product discount as a graduation reward.
  • After a membership is activated: The MemberPress integration covers signup and payment events, so a welcome coupon for new members takes about 10 minutes to set up.

If you want a detailed walkthrough of the specific case where a coupon follows a first purchase, we have a dedicated post on how to send a coupon after a customer’s first WooCommerce order that covers the first-order filter logic in full.

Also Worth Knowing About

Pollify is wpRigel’s poll, survey, and quiz plugin for WordPress. It builds natively inside the block editor as real Gutenberg blocks, so there are no shortcodes to paste and no separate interface to learn. If you want to survey customers after a purchase, Pollify sits right inside the post editor alongside your content.

Commandify is a command palette for the WordPress admin. Press Cmd or Ctrl plus K to jump anywhere, search orders, customers, and products, and run admin actions without navigating menus. It is the only command palette with real WooCommerce order, product, and customer commands built in, which makes it genuinely useful for anyone managing a busy store.

Next Steps

If your goal is a unique coupon per customer sent automatically on a trigger, the visual workflow approach gets you there in under 20 minutes with no code and a full execution log to audit. If you need scheduled bulk generation for a campaign, the PHP method gives you that at zero plugin cost, provided you are comfortable maintaining the code. Start with the method that matches your use case, test it on a staging order before going live, and use the execution log to confirm every step ran as expected.

Try Krom Automation free on WordPress.org

Frequently Asked Questions

How do I automatically apply a WooCommerce coupon without customers entering a code?

Add ?coupon_code=YOURCODE to any cart or checkout URL. When a customer clicks the link, WooCommerce applies the coupon automatically at checkout. This works with any existing coupon and requires no plugin.

Can I auto-generate a unique coupon code for each customer after they order?

Yes. Use an Order Completed trigger connected to a Create Coupon action. Set the coupon code field to a combination of a static prefix and the {{order_id}} merge tag.

That produces a unique, readable code for every order. Set the usage limit to 1 so the code cannot be shared.

How do I create a WooCommerce coupon programmatically using PHP?

Use wp_insert_post() with post_type set to shop_coupon, then set coupon properties via update_post_meta(). The key meta fields are discount_type, coupon_amount, usage_limit, and date_expires. The full list of meta keys is in the WooCommerce coupon documentation.

How do I set an automatic expiry date on generated WooCommerce coupons?

In a PHP-generated coupon, set the date_expires meta value to a Unix timestamp, for example strtotime( '+30 days' ). In a visual workflow, enter a fixed date in the expiry field of the Create Coupon action, or use a delay node before a secondary action that updates the coupon record.

What is the difference between Order Created and Order Completed as a trigger for coupon automation?

Order Created fires when payment is confirmed, before fulfillment. Order Completed fires when the order status changes to “Completed,” which is usually after you ship the item or manually update the status.

Use Order Created for instant post-purchase coupons. Use Order Completed for loyalty rewards tied to delivery or fulfillment.