WooCommerce has vast of customization by which web shop owner can build the online web shop the way they want. By default WooCommerce has “ADD TO CART” button on all products. In this article we going to guide you how you can add the “BUY NOW” button with the “ADD TO CART” button on single and variable WooCommerce products.
But if you want to change “ADD TO CART” button to “ADDED TO CART” when product added in cart then you can click here.
Step by step guide to add “BUY NOW” button on Single and Variable Product.
- Create child theme and click on theme editor in “Appearance” option on left menu.
- Add the below code on your functions.php file. You can replace text “Buy now” which another text you want.
/* Dynamic Button for Simple & Variable Product */
/**
* Main Functions
*/
function sbw_wc_add_buy_now_button_single()
{
global $product;
printf( '<button id="sbw_wc-adding-button" type="submit" name="sbw-wc-buy-now" value="%d" class="single_add_to_cart_button buy_now_button button alt">%s</button>', $product->get_ID(), esc_html__( 'Buy Now', 'sbw-wc' ) );
}
add_action( 'woocommerce_after_add_to_cart_button', 'sbw_wc_add_buy_now_button_single' );
/*** Handle for click on buy now ***/
function sbw_wc_handle_buy_now()
{
if ( !isset( $_REQUEST['sbw-wc-buy-now'] ) )
{
return false;
}
WC()->cart->empty_cart();
$product_id = absint( $_REQUEST['sbw-wc-buy-now'] );
$quantity = absint( $_REQUEST['quantity'] );
if ( isset( $_REQUEST['variation_id'] ) ) {
$variation_id = absint( $_REQUEST['variation_id'] );
WC()->cart->add_to_cart( $product_id, 1, $variation_id );
}else{
WC()->cart->add_to_cart( $product_id, $quantity );
}
wp_safe_redirect( wc_get_checkout_url() );
exit;
}
add_action( 'wp_loaded', 'sbw_wc_handle_buy_now' );
/* Dynamic Button for Simple & Variable Product Closed */
- Once code added, click on SAVE and check on website.
To change the “SELECT OPTIONS” text on shop page for variable products to “ADD TO CART“, you can follow below steps.
- Add below code in same functions.php file.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'change_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_add_to_cart_text', 'change_cart_button_text', 10, 2 );
function change_cart_button_text( $button_text, $product ) {
if ( $product->is_type( 'variable' ) )
$button_text = __('Buy Now', 'woocommerce');
return $button_text;
}
Above code is tested and Worked perfectly
So that’s all you need to do to get WooCommerce works for you. Let us know how it works for you. For instant support ping us on whatsapp and comment in comment box.
Happy coding!
[caldera_form id=”CF60ca2e3ccf64f”]