1
votes

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/

4
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
@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
Did you solve your problem? I would like to achive the same goalJosé Augustinho

4 Answers

4
votes

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/

2
votes

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' );
0
votes

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
votes
<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>