How to Add Buy Now Button WooCommerce Without Plugin

Add to Buy Now Button Without Plugin In a recent WooCommerce development project, I was asked by a client to add a buy now button on the product page, I needed to create an easy and quick way to add buy now button WooCommerce without a plugin.

I created the solution I will share with you in this quick guide and also WooCommerce add buy now button code snippet that you can reuse in your WooCommerce development projects or customization of WooCommerce to help you save time and be more productive.

Add Buy Now Button WooCommerce Without Plugin

To create the add to cart or buy now button in WooCommerce single product page as shown in the image below, I need to use the action filter and created the code snippet shared below:

/* Create Buy Now Button dynamically after Add To Cart button */
function add_content_after_addtocart() {

// get the current post/product ID
$current_product_id = get_the_ID();

// get the product based on the ID
$product = wc_get_product( $current_product_id );

// get the "Checkout Page" URL
$checkout_url = WC()->cart->get_checkout_url();

// run only on simple products
if( $product->is_type( 'simple' ) ){
echo '<div class="clear-sec">';
echo '</div>';
echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="buy-now button">Buy Now</a>';
//echo '<a href="'.$checkout_url.'" class="buy-now button">Buy Now</a>';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );

You can now use this code in the functions.php of your theme or in your plugin development to showcase the add buy now button without using a plugin. You can also style the button like how I did in the image above by adding the following CSS styles:

.buy-now.button {
max-width: 200px;
text-align: center;
background: #ffa500 !important;
color: #fff !important;
font-weight: bold !important;
}

You can change the colors to match your preferred color of the buy now button.

Conclusion

In this post, we have highlighted how you can add a buy now button to your WooCommerce product page without using a plugin.

If you need further assistance in fixing this issue, I can quickly check your theme and advise what is the best way to get this done.  I would be glad to help you., feel free to reach out for further assistance, you can get in touch for a free consultation.

Similar Articles

  1. How to Check If Product Page In WooCommerce