I wish to ADD a "buy now" button on product page , and redirect to checkout page after clicking it without adding the product into cart. I noticed that a similar question was been asked before Woocommerce - Add To Cart and Buy Now buttons on Product Pages
But this method only redirect to the checkout page but still adding the corresponding product into cart.
How can I achieve this? Many thanks.
------update 10/7/2015----------
I think the best way to do it is to create another cart and checkout instance, But I just don't know how to implement it , Could anyone help me ?
35 Answers
You can use the WooCommerce hook woocommerce_after_add_to_cart_button. This hook will add content after the "Add To Cart" button.
If the customer clicks on this button, the product should get added to the cart, and the customer should be send to the checkout page.
Add the below code in your child theme functions.php
/* 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 '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'">Buy Now</a>'; //echo '<a href="'.$checkout_url.'">Buy Now</a>'; } } add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' ); 1I once used this code
<?php $add_to_cart = do_shortcode('[add_to_cart_url]'); ?> <a href="'. $add_to_cart .'"><img src="'. get_template_directory_uri() . '/img/small-cart.png" />Buy Now</a> WooCommerce also has a large list of shortcodes to make developing e-comm themes easy. You can have a look here:
1This snippet of code should work fine with both simple & Variable products, that create a dynamic button next to Add to Cart Button in Woocommerce.
Add this code in your child theme 'functions.php' file and you are Good to go.
/* Dynamic Button for Simple & Variable Product */ /** * Main Functions */ function sbw_wc_add_buy_now_button_single() { global $product; printf( '<button type="submit" name="sbw-wc-buy-now" value="%d">%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 */ 1Try this code in your theme functions.php it will redirect you to checkout page after click on add to cart button
function redirect_to_checkout() { return WC()->cart->get_checkout_url(); } <a href="<?php echo esc_url(home_url( '/' ));?>cart/?add-to-cart=<?php the_ID();?>"><?php esc_html_e('Buy it now!', 'your-domain'); ?></a>