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.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Check If Product is Variable In WooCommerce
- How to Install Classic Editor In WordPress
- How to Add Product SKU In WooCommerce
- WooCommerce Get Total Spent By Customer
- How to Delete a Category in WordPress
- WordPress Get Post Category Snippet & Guide
- How to Get Product Name WooCommerce
- How to Redirect a WordPress Page Without Plugins?
- How to Add CAPTCHA WooCommerce Login
- How to Redirect User If Not Logged in WordPress » Page Redirect
- How to Remove Product Category from URL WooCommerce Without Plugin
- How Get Order meta_data In WooCommerce
- How to Get Last Order by User Id WooCommerce
- How to Change Out Of Stock Text in WooCommerce
- How to Check If Current Page Is Category WordPress
- How to Hide Additional Information Tab In WooCommerce
- How to Create Number Pagination in WordPress Without Using Plugin
- How to Check If Product Page In WooCommerce