Add below code in functions.php file.
Buy and empty Cart
// Add Buy Now button on product page
add_action( 'woocommerce_after_add_to_cart_button', 'add_buy_now_button' );
function add_buy_now_button() {
global $product;
echo '<a href="' . esc_url( wc_get_checkout_url() ) . '?buy-now=' . $product->get_id() . '" class="button alt">Buy Now</a>';
}
// Handle Buy Now button redirect to checkout page
add_action( 'template_redirect', 'handle_buy_now_redirect' );
function handle_buy_now_redirect() {
if ( isset( $_GET['buy-now'] ) ) {
// Remove all products from the cart
WC()->cart->empty_cart();
// Add the specified product to the cart
$product_id = intval( $_GET['buy-now'] );
WC()->cart->add_to_cart( $product_id );
// Redirect to checkout page
wp_safe_redirect( wc_get_checkout_url() );
exit;
}
}
Buy Product + All product on Cart
// Add Buy Now button on product page
add_action( 'woocommerce_after_add_to_cart_button', 'add_buy_now_button' );
function add_buy_now_button() {
global $product;
echo '<a href="' . esc_url( wc_get_cart_url() ) . '?buy-now=' . $product->get_id() . '" class="button alt buy-now-button">Buy Now</a>';
}
// Add CSS to change the color of the button
add_action( 'wp_enqueue_scripts', 'custom_buy_now_button_styles' );
function custom_buy_now_button_styles() {
echo '<style>
.buy-now-button {
background-color: blue !important;
color: white !important;
}
</style>';
}
// Handle Buy Now button redirect to cart page
add_action( 'template_redirect', 'handle_buy_now_redirect' );
function handle_buy_now_redirect() {
if ( isset( $_GET['buy-now'] ) ) {
// Add the specified product to the cart
$product_id = intval( $_GET['buy-now'] );
WC()->cart->add_to_cart( $product_id );
// Redirect to cart page
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
#Buy Product +Shop Page to Singal product page+ All product on Cart +
// Add Buy Now button on product page
add_action( 'woocommerce_after_add_to_cart_button', 'add_buy_now_button' );
add_action('woocommerce_after_shop_loop_item', 'add_buy_now_button');
function add_buy_now_button() {
global $product;
echo '<a href="' . esc_url( wc_get_cart_url() ) . '?buy-now=' . $product->get_id() . '" class="button alt buy-now-button">Buy Now</a>';
}
// Handle Buy Now button redirect to cart page
add_action( 'template_redirect', 'handle_buy_now_redirect' );
function handle_buy_now_redirect() {
if ( isset( $_GET['buy-now'] ) ) {
// Add the specified product to the cart
$product_id = intval( $_GET['buy-now'] );
WC()->cart->add_to_cart( $product_id );
// Redirect to cart page
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
Redirect to cart not checkout + Buy Product + All product on Cart
// Add Buy Now button on product page
add_action( 'woocommerce_after_add_to_cart_button', 'add_buy_now_button' );
function add_buy_now_button() {
global $product;
echo '<a href="' . esc_url( wc_get_cart_url() ) . '?buy-now=' . $product->get_id() . '" class="button alt">Buy Now</a>';
}
// Handle Buy Now button redirect to cart page
add_action( 'template_redirect', 'handle_buy_now_redirect' );
function handle_buy_now_redirect() {
if ( isset( $_GET['buy-now'] ) ) {
// Add the specified product to the cart
$product_id = intval( $_GET['buy-now'] );
WC()->cart->add_to_cart( $product_id );
// Redirect to cart page
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
2. Add CSS Code.
#wpamit-adding-button {
margin-left:20px;
background-color:blue
}
@media only screen and (max-width: 600px) {
#wpamit-adding-button {
margin:10px;
}
}

