Woocommerce apply coupon workflow

Tech Articles | July 31, 2025 | Blog, Coding, WooCommerce, Wordpress

There was a post today on a popular Facebook group about WooCommerce workflows and I said I would look at my snippet library as I thought i would have one that carried out the following workflow.

“Add to Cart & Go to Checkout” link, I want a free gift coupon to be automatically added to their cart.”  I if the customer enters through a specific landing page (i.e., part of a funnel).

My Initial reply was just to add an add to cart button for that product with the URL params and i gave some options for plugins that could achieve this but did say I would come back to my repository and post back if I had something.

I had a look when I came back and I had the perfect example which is what this article is about and in this it allows the following behaviour customised url-parms for checkout product and coupon and the ability to make it a direct to checkout product (i.e. empty the cart and get it ready to checkout)

As well as the functionality it also add’s two additional options to the admin area in general settings it adds the customisable url paramater query variables.

Woo coupon direct checkout params settings

In addition to this it also adds an option to each product for it to be a direct checkout, this means it will empty the cart and let that item be checkout out i.e with the coupon great for funnel pages.

Woo coupon direct checkout

So in the default settings with the direct to checkout option checked is that when ever this product is added to the cart in the normal fasion it will clear the cart before hand.

With the default query variables https://domain.com?add_product=64&apply_coupon=discount and you will automatically clear the cart and add the item to the cart, add the coupon and be ready to check straight out regardless of this direct to checkout option.

In summary the direct to checkout option only applies to normal shopping not parameter based links

To achieve this I use a simple snippet through WPcodeBox

<?php
/**
 * Plugin Name: WC Direct Checkout + URL Params
 * Description: Adds per-product "direct to checkout", apply coupon via URL and auto-add product via URL with configurable parameter names.
 * Version: 1.5
 */

// 1) PRODUCT META: "Direct to checkout" checkbox
add_action('woocommerce_product_options_general_product_data', 'wc_add_direct_checkout_field');
function wc_add_direct_checkout_field() {
    woocommerce_wp_checkbox([
        'id'          => '_direct_checkout',
        'label'       => __('Direct to checkout', 'woocommerce'),
        'description' => __('Clears cart and sends customer straight to checkout when added.', 'woocommerce'),
    ]);
}
add_action('woocommerce_process_product_meta', 'wc_save_direct_checkout_field');
function wc_save_direct_checkout_field($post_id) {
    update_post_meta($post_id, '_direct_checkout', isset($_POST['_direct_checkout']) ? 'yes' : 'no');
}

// 2) DIRECT-CHECKOUT FLOW (empties cart on add-to-cart)
add_filter('woocommerce_add_to_cart_validation', 'wc_direct_checkout_validation', 10, 3);
function wc_direct_checkout_validation($passed, $product_id) {
    if (!is_admin() && isset($_REQUEST['add-to-cart']) && get_post_meta($product_id, '_direct_checkout', true) === 'yes') {
        WC()->cart->empty_cart();
    }
    return $passed;
}
add_filter('woocommerce_add_to_cart_redirect', 'wc_direct_checkout_redirect');
function wc_direct_checkout_redirect($url) {
    if (!empty($_REQUEST['add-to-cart'])) {
        $pid = absint($_REQUEST['add-to-cart']);
        if (get_post_meta($pid, '_direct_checkout', true) === 'yes') {
            return wc_get_checkout_url();
        }
    }
    return $url;
}

// 3) ADMIN SETTINGS: Add section "Direct Checkout Params" under coupon settings
add_filter('woocommerce_general_settings', 'wc_url_params_settings', 9999);
function wc_url_params_settings($settings) {
    $new_settings = [];
    $insert_after_id = 'woocommerce_enable_coupons';
    foreach ($settings as $section) {
        $new_settings[] = $section;
        if (isset($section['id']) && $section['id'] === $insert_after_id) {
            $new_settings[] = [ 'title' => __('Direct Checkout Params', 'woocommerce'), 'type' => 'title', 'id' => 'wc_direct_checkout_params_section' ];
            $new_settings[] = [ 'title' => __('URL Coupon Parameter', 'woocommerce'), 'id' => 'wc_apply_coupon_param', 'type' => 'text', 'default' => 'apply_coupon', 'desc' => __('Query var for coupon code, e.g. ?apply_coupon=CODE', 'woocommerce'), 'autoload' => false ];
            $new_settings[] = [ 'title' => __('URL Product Parameter', 'woocommerce'), 'id' => 'wc_add_product_param', 'type' => 'text', 'default' => 'add_product', 'desc' => __('Query var for auto-adding product, e.g. ?add_product=80', 'woocommerce'), 'autoload' => false ];
            $new_settings[] = [ 'type' => 'sectionend', 'id' => 'wc_direct_checkout_params_section' ];
        }
    }
    return $new_settings;
}

// 4) CAPTURE URL PARAMS & STORE IN SESSION
add_action('init', 'wc_capture_url_params');
function wc_capture_url_params() {
    if (!WC()->session) return;
    $cparam = get_option('wc_apply_coupon_param', 'apply_coupon');
    if (isset($_GET[$cparam])) {
        $code = wc_clean(wp_unslash($_GET[$cparam]));
        WC()->session->set('wc_url_coupon', $code);
    }
    $pparam = get_option('wc_add_product_param', 'add_product');
    if (isset($_GET[$pparam])) {
        $pid = absint($_GET[$pparam]);
        if ($pid) {
            WC()->session->set('wc_url_product', $pid);
            WC()->session->set('wc_url_product_added', false);
        }
    }
}

// 5) APPLY COUPON on cart load (only if exists)
add_action('woocommerce_cart_loaded_from_session', 'wc_apply_stored_coupon');
function wc_apply_stored_coupon($cart) {
    $code = WC()->session->get('wc_url_coupon');
    if (!$code) return;

    // Only apply if coupon exists
    if (!wc_get_coupon_id_by_code($code)) {
        WC()->session->__unset('wc_url_coupon');
        return;
    }

    // Apply if not yet applied
    if (!$cart->has_discount($code)) {
        $cart->apply_coupon($code);
        // Clear notices so no errors remain
        wc_clear_notices();
    }
}

// 6) AUTO-ADD PRODUCT & FORCE SINGLE QTY + REDIRECT
add_action('template_redirect', 'wc_auto_add_product_and_redirect');
function wc_auto_add_product_and_redirect() {
    if (is_admin()) return;
    $pid = WC()->session->get('wc_url_product');
    $added = WC()->session->get('wc_url_product_added');
    if ($pid && !$added) {
        // always ensure only one item: clear cart
        WC()->cart->empty_cart();
        // add single quantity
        WC()->cart->add_to_cart($pid, 1);
        WC()->session->set('wc_url_product_added', true);
        // redirect to checkout
        wp_safe_redirect(wc_get_checkout_url());
        exit;
    }
}
PHP

You can see this process in action in the video below

Support the Author

buy me a coffee
Really Useful Plugin Logo
Wpvideobank temp
Appoligies for any spelling and grammer issue. As a dyslexic i need to rely on tools for this they like me are not perfect but I do try my best