3

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 ?

http://wordpress.org/extend/plugins/woocommerce/

3
  • 1
    Your requirement isn't making sense. What is the point in redirecting the customer to an empty checkout page? If the cart is empty, checkout will redirect back to cart. Please provide some more info about why you are wanting to achieve this, there might be another solution to the problem.
    – Anand Shah
    Commented Sep 14, 2015 at 8:58
  • @Anand Thx for reply. My intention of this 'buy now' button is to skip through the Add to cart function. For example, if there are already products A,B in the cart , and I click the 'buy now' button on another product page of C , A and B in the cart will be still in the cart and remain not checkout, Only C will be in the product list of the check out page and also C is not adding into cart. Does that make sense?
    – charlie_k
    Commented Sep 14, 2015 at 23:09
  • Did you solve your problem? I would like to achive the same goal Commented Aug 25, 2020 at 19:04

5 Answers 5

6

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.'" 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' );
1
  • This works really well for simple products, but doesn't seem to display on variable products. Is that a known limitation, and do variable products have to be handled differently? Commented May 7, 2021 at 5:51
5

This 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 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 */

Credits : https://profiles.wordpress.org/navarroido/

3
  • It works perfectly for simple and variable products. But it doesn't take the quantity selection into consideration. Do you know how it can be fixed?
    – M.S Shohan
    Commented Aug 21, 2022 at 3:12
  • @M.SShohan intstead of WC()->cart->add_to_cart( $product_id, 1, $variation_id ); write WC()->cart->add_to_cart( $product_id, $quantity, $variation_id ); and it will handle the quantity for variable products
    – Barnabas
    Commented Apr 4 at 12:09
  • If this code is just adding another Add to Cart button, disable the AJAX Add to Cart function in your settings. Commented Sep 2 at 11:11
4

I once used this code

<?php $add_to_cart = do_shortcode('[add_to_cart_url id="25"]'); ?>
<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: http://docs.woothemes.com/document/woocommerce-shortcodes/

1
  • 3
    But I want to redirect to checkout page without adding the product into cart.
    – charlie_k
    Commented Sep 14, 2015 at 5:47
0

Try 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();
}
0
<a href="<?php echo esc_url(home_url( '/' ));?>cart/?add-to-cart=<?php the_ID();?>" class="btn btn-primary"><?php esc_html_e('Buy it now!', 'your-domain'); ?></a>

Not the answer you're looking for? Browse other questions tagged or ask your own question.