Site icon Rashed Hossain

Add a “Buy Now” Button with One-Click Checkout in WooCommerce

Offering a faster purchase path can significantly improve conversion rates, especially for single-product purchases. With this simple snippet, you can add a “Buy Now” button beside the default “Add to Cart” button on your WooCommerce store, enabling a one-click checkout experience.

What This Does:


Code Overview

1. Display Buy Now Button

This part hooks into the product page after the “Add to Cart” button and appends a “Buy Now” button with a custom query string.

add_action('woocommerce_after_add_to_cart_button', 'one_click_checkout_button');
function one_click_checkout_button() {
    global $product;

    if ($product->is_type('simple')) {
        $url = esc_url(add_query_arg([
            'one_click_checkout' => 'yes',
            'product_id' => $product->get_id()
        ], home_url()));
        echo '<a href="' . $url . '" class="button alt" style="margin-left:10px;">Buy Now</a>';
    }
}

2. Handle Redirection and Cart Logic

When the “Buy Now” button is clicked, it triggers a template_redirect action that:

add_action('template_redirect', 'handle_one_click_checkout');
function handle_one_click_checkout() {
    if (!isset($_GET['one_click_checkout']) || $_GET['one_click_checkout'] !== 'yes') return;

    $product_id = intval($_GET['product_id']);
    if (!$product_id) return;

    WC()->cart->empty_cart();
    WC()->cart->add_to_cart($product_id);

    wp_redirect(wc_get_checkout_url());
    exit;
}

Use Case

Perfect for:

This approach improves speed and convenience for users who want a streamlined buying experience.

Want help turning this into a plugin or adding variations and grouped product support?

Exit mobile version