PHP /**
* Snippet Name: Add Buy Now Button
* Snippet Author: coding-bunny.com
* Description: Adds a "Buy Now" button to single product pages that empties the cart, adds the current product, and redirects to checkout.
* Version: 1.1.0
*/
if ( ! defined ( ' ABSPATH ' ) ) {
exit;
}
function cbbnb_buy_now_btn () {
global $ product ;
if ( ! $ product || ! $ product -> is_purchasable () || ! $ product -> is_in_stock () ) {
return;
}
$ nonce = wp_create_nonce ( ' cbbnb_buy_now_nonce ' ) ;
printf (
' <button id="cbbnb-buy-now" type="submit" name="cbbnb-buy-now" value="%d" data-nonce="%s" class="single_add_to_cart_button cbbnb_buy_now_btn button alt">%s</button> ' ,
esc_attr ( $ product -> get_id () ),
esc_attr ( $ nonce ),
esc_html__ ( ' Buy Now ' , ' cbbnb ' )
) ;
}
add_action ( ' woocommerce_after_add_to_cart_button ' , ' cbbnb_buy_now_btn ' ) ;
function cbbnb_buy_now_handle () {
if ( ! isset ( $ _REQUEST [ ' cbbnb-buy-now ' ] ) ) {
return;
}
$ nonce = isset ( $ _REQUEST [ ' _wpnonce ' ] ) ? sanitize_text_field ( wp_unslash ( $ _REQUEST [ ' _wpnonce ' ] ) ) : '' ;
if ( ! wp_verify_nonce ( $ nonce , ' cbbnb_buy_now_nonce ' ) ) {
wp_die (
esc_html__ ( ' Security check failed. Please try again. ' , ' cbbnb ' ),
esc_html__ ( ' Security Error ' , ' cbbnb ' ),
array ( ' response ' => 403 )
) ;
}
$ product_id = absint ( $ _REQUEST [ ' cbbnb-buy-now ' ] ) ;
if ( ! $ product_id ) {
wc_add_notice ( __ ( ' Invalid product. ' , ' cbbnb ' ), ' error ' ) ;
return;
}
$ product = wc_get_product ( $ product_id ) ;
if ( ! $ product || ! $ product -> is_purchasable () || ! $ product -> is_in_stock () ) {
wc_add_notice ( __ ( ' This product cannot be purchased. ' , ' cbbnb ' ), ' error ' ) ;
return;
}
$ quantity = isset ( $ _REQUEST [ ' quantity ' ] ) ? absint ( $ _REQUEST [ ' quantity ' ] ) : 1 ;
if ( $ quantity < 1 ) {
$ quantity = 1 ;
}
if ( ! $ product -> has_enough_stock ( $ quantity ) ) {
wc_add_notice ( __ ( ' Not enough stock available. ' , ' cbbnb ' ), ' error ' ) ;
return;
}
try {
WC () -> cart -> empty_cart () ;
if ( isset ( $ _REQUEST [ ' variation_id ' ] ) && $ _REQUEST [ ' variation_id ' ] ) {
$ variation_id = absint ( $ _REQUEST [ ' variation_id ' ] ) ;
$ variation = wc_get_product ( $ variation_id ) ;
if ( ! $ variation || ! $ variation -> is_purchasable () ) {
wc_add_notice ( __ ( ' Invalid product variation. ' , ' cbbnb ' ), ' error ' ) ;
return;
}
$ attributes = array () ;
if ( isset ( $ _REQUEST [ ' attribute ' ] ) && is_array ( $ _REQUEST [ ' attribute ' ] ) ) {
foreach ( $ _REQUEST [ ' attribute ' ] as $ key => $ value ) {
$ attributes [ sanitize_key ( $ key ) ] = sanitize_text_field ( wp_unslash ( $ value ) ) ;
}
}
$ cart_item_key = WC () -> cart -> add_to_cart (
$ product_id ,
$ quantity ,
$ variation_id ,
$ attributes
) ;
} else {
$ cart_item_key = WC () -> cart -> add_to_cart ( $ product_id , $ quantity ) ;
}
if ( ! $ cart_item_key ) {
wc_add_notice ( __ ( ' Failed to add product to cart. ' , ' cbbnb ' ), ' error ' ) ;
return;
}
wp_safe_redirect ( wc_get_checkout_url () ) ;
exit;
} catch ( Exception $ e ) {
if ( defined ( ' WP_DEBUG ' ) && WP_DEBUG ) {
error_log ( ' Buy Now Button Error: ' . $ e -> getMessage () ) ;
}
wc_add_notice ( __ ( ' An error occurred. Please try again. ' , ' cbbnb ' ), ' error ' ) ;
return;
}
}
add_action ( ' wp_loaded ' , ' cbbnb_buy_now_handle ' ) ;
function cbbnb_buy_now_js () {
if ( ! is_product () ) {
return;
}
?>
< script type = " text/javascript " >
jQuery ( document ) . ready ( function ( $ ) {
$ ( ' #cbbnb-buy-now ' ) . on ( ' click ' , function ( e ) {
var $ form = $ ( this ) . closest ( ' form.cart ' ) ;
var nonce = $ ( this ) . data ( ' nonce ' ) ;
if ( nonce && !$ form . find ( ' input[name="_wpnonce"] ' ) . length ) {
$ form . append ( ' <input type="hidden" name="_wpnonce" value=" ' + nonce + ' "> ' ) ;
}
}) ;
}) ;
</ script >
<? php
}
add_action ( ' wp_footer ' , ' cbbnb_buy_now_js ' ) ;
function cbbnb_buy_now_css () {
if ( ! is_product () ) {
return;
}
?>
< style type = " text/css " >
. cbbnb_buy_now_btn {
margin - left : 10 px ! important ;
background - color : #ff6b35 !important;
border - color : #ff6b35 !important;
}
. cbbnb_buy_now_btn : hover {
background - color : #e55a2b !important;
border - color : #e55a2b !important;
}
</ style >
<? php
}
add_action ( ' wp_head ' , ' cbbnb_buy_now_css ' ) ;
How To Implement This Solution?