# Add a "Buy Now" Button Next to Add to Cart

> Adds a second button on product pages that adds the product to the cart and takes the shopper straight to checkout, while the normal add to cart button keeps working.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, checkout, buttons, conversion
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-buy-now-button/

## Code

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

    if ( ! $product instanceof WC_Product || ! $product->is_purchasable() || ! $product->is_in_stock() || $product->is_type( array( 'external', 'grouped' ) ) ) {
        return;
    }

    echo '<button type="submit" name="wpide_buy_now" value="1" class="button alt wpide-buy-now" style="margin-left:8px;">' . esc_html( 'Buy now' ) . '</button>';

    if ( $product->is_type( 'simple' ) ) {
        printf( '<input type="hidden" name="add-to-cart" value="%d">', (int) $product->get_id() );
    }
} );

add_filter( 'woocommerce_add_to_cart_redirect', function ( $url ) {
    if ( ! empty( $_REQUEST['wpide_buy_now'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
        return wc_get_checkout_url();
    }

    return $url;
}, 20 );
```

## Notes

Works for simple and variable products on the classic product template.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Add a "Buy Now" Button Next to Add to Cart" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
