The “Buy now” button allows the user to skip the card page when buying a product and go directly to the checkout page and purchase the product faster.
In this tutorial, you will learn how to add a Buy Now button to your WooCommerce product page without a plugin. We will also see how to customize this button and place it only on products in certain categories.

buy now button for woocommerce

Add buy now button in WooCommerce without plugins

Using the following code, you can add the “Buy now” button to single and variable products in WooCommerce.

Buy now button WooCommerce code

In the WordPress dashboard, go to Appearance ➡ Theme File Editor and copy the following code into the theme’s functions.php file and save it.

You must create a child theme before making any changes to functions.php file. Otherwise, the applied changes will be lost after each update.
Create child theme in WordPress step by step [without plugin]

As an alternative method, you can use the Code Snippets plugin to insert your codes into WordPress.

/* buy now button woocommerce code by WPcookie
 * update: https://redpishi.com/wordpress-tutorials/buy-now-button-woocommerce/
 */
add_action( 'wp_footer', function(){ if(!is_admin()) { 

// Change button color from here
$color = ""; 
	
?>
<style>
a.custom-checkout-btn {
    margin: 0px 5px!important;
}
</style>
<?php
if ( $color != "" ){
?>
<style>
a.custom-checkout-btn {
    background: <?php echo $color; ?>!important;
    transition: 0.4s filter ease; 
}
a.custom-checkout-btn:hover {
    filter: saturate(2);
}
</style>
<?php }}});

add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_addtocart_and_checkout' );
function add_custom_addtocart_and_checkout() {	
    global $product;
	
	// conditional tags
	$condition = $product->is_type( 'simple' ) || $product->is_type( 'variable' );
	
    $addtocart_url = wc_get_checkout_url().'?add-to-cart='.$product->get_id();
    $button_class  = 'single_add_to_cart_button button alt custom-checkout-btn';

	// Change the text of the buy now button from here
    $button_text   = __("Buy now", "woocommerce");
	
    if( $product->is_type( 'simple' ) ) :		
    ?>
    <script>
    jQuery(function($) {
        var url    = '<?php echo $addtocart_url; ?>',
            qty    = 'input.qty',
            button = 'a.custom-checkout-btn';

        // On input/change quantity event
        $(qty).on('input change', function() {
            $(button).attr('href', url + '&quantity=' + $(this).val() );
        });
    });
    </script>
    <?php
    elseif( $product->is_type( 'variable' ) ) :	
    $addtocart_url = wc_get_checkout_url().'?add-to-cart=';
    ?>
    <script>
    jQuery(function($) {
        var url    = '<?php echo $addtocart_url; ?>',
            vid    = 'input[name="variation_id"]',
            pid    = 'input[name="product_id"]',
            qty    = 'input.qty',
            button = 'a.custom-checkout-btn';
        setTimeout( function(){
            if( $(vid).val() != '' ){
                $(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() );
            }
        }, 300 );
        $(qty).on('input change', function() {
            if( $(vid).val() != '' ){
                $(button).attr('href', url + $(vid).val() + '&quantity=' + $(this).val() );
            }
        });
        $('.variations_form').on('change blur', 'table.variations select', function() {
            if( $(vid).val() != '' ){
                $(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() );
            }
        });
    });
    </script>
    <?php			
    endif;
	if ($condition) {
		echo '<a href="'.$addtocart_url.'" class="'.$button_class.'">'.$button_text.'</a>';
	}   
}

After adding the above code to the site theme’s “functions.php” file, you will see the “Buy now” button on the product pages.

Customize the “Buy now” button

Find the section below in the code and enter the desired color code to modify the “Buy now” button’s color.

// Change button color from here
$color = "#ff6a00"; 

To change the text of the “Buy now” button, find the following section of the code and replace “Buy now” with your desired wording.

// Change the text of the buy now button from here
$button_text   = __("Buy now", "woocommerce");

Add buy now to some categury

To display the “Buy Now” button on products from a specific category, Find the $condition variable and replace it with the conditional tag below.
In the code below, instead of ‘tshirts’, write the Slug of category you want.

// conditional tags
$condition = has_term( [ 'tshirts' ], 'product_cat' ) ;

From now on, the “buy now” button will only be displayed for T-shirts.
If you want more than one category, separate each category Slug with a comma.

// conditional tags
$condition = has_term( [ 'tshirts', 'hoodies', 'decor' ], 'product_cat' );

To learn more about conditional tags and how to use them in WordPress, read the following article.
https://redpishi.com/wordpress-tutorials/insert-code-in-wordpress-without-plugins/#insert-php-code-in-pages-with-given-terms

If this article is difficult for you to read in text, you can watch the video version below.

Share this post
Maya
Maya

Hi, my name is Maya and I’m a WordPress plugin developer. I created this website to share some of the helpful codes that I’ve used in my own projects.
If you’re looking for a custom plugin for your website, you can contact me by clicking on Hire a developer in the menu. I’d love to hear from you.

Articles: 53

9 Comments

  1. Thanks a lot for this. But the buy now is still acting as add to cart. Instead of just redirecting to checkout page. It still adding to cart. Any help pls

  2. Hi there,

    Thanks so much for this! I would like to know how I can make the \”Buy now\” button a little smaller? I tried changing the margin px value from 5 to 4. I didn\’t notice any change in the size of the button though.

    Appreciate it!
    Albert

    • Hi Albert
      If you want to customize the size of the button, this code will do the trick:

      a.custom-checkout-btn {
      width: 80px;
      height: 20px;
      flex: unset;
      }

      • Thanks so much!
        I\’ve actually just managed to add a max height to the code 🙂
        I followed the same way the other lines were added and after a few tries, managed to get it right.

        I appreciate your fast reply!

  3. hi i have a problme with the code it s not the bouton but\’s it s not direct me to place order page directly my buton have the same fonction of add to cart button

  4. Hai, please help me on this. This code works perfectly fine for me, but I am facing one issue with the external or affiliated products. I don\’t want this Buy Now button on External Products as it return a message in the cart window saying \”This product cannot be purchased\” O how do I remove this button from external products?

    • Hey Dhanuan!
      Thanks a lot for your comment.
      I made some changes to the code, so now the Buy Now button will only show up for simple and variable products.

Leave a Reply

Your email address will not be published. Required fields are marked *