• Resolved fizcoder

    (@fizshoemaker)


    Hi, I am attempting to add a buy now button in Woocommerce on the product page so there are two buttons:
    1. add to cart
    2. buy now (which will add the product to the cart and redirect to checkout)

    I still want add to cart to function as usual.

    How can I achieve this? Many thanks.

    https://www.ads-software.com/extend/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter fizcoder

    (@fizshoemaker)

    I managed to resolve this by finding this blog post https://samplacette.com/skip-shopping-cart-in-woocommerce/.

    If anyone else finds that they are struggling to implement this, this is how I did it (might not be the best solution but it works for me):

    I copied the following text into my theme functions.php

    /** * Set cart item quantity action *
        * Only works for simple products (with integer IDs, no colors etc) *
        * @access public
        * @return void */
        function woocommerce_set_cart_qty_action() {
            global $woocommerce;
            foreach ($_REQUEST as $key => $quantity) {
            // only allow integer quantities
            if (! is_numeric($quantity)) continue;
    
                // attempt to extract product ID from query string key
                $update_directive_bits = preg_split('/^set-cart-qty_/', $key);
                if (count($update_directive_bits) >= 2 and is_numeric($update_directive_bits[1])) {
                    $product_id = (int) $update_directive_bits[1];
                    $cart_id = $woocommerce->cart->generate_cart_id($product_id);
                    // See if this product and its options is already in the cart
                    $cart_item_key = $woocommerce->cart->find_product_in_cart( $cart_id );
                    // If cart_item_key is set, the item is already in the cart
                    if ( $cart_item_key ) {
                        $woocommerce->cart->set_quantity($cart_item_key, $quantity);
                    } else {
                        // Add the product to the cart
                        $woocommerce->cart->add_to_cart($product_id, $quantity);
                    }
                }
            }
        }
    
        add_action( 'init', 'woocommerce_set_cart_qty_action' );

    And then I modified *theme*/woocommerce/single-product/add-to-cart/simple.php (make sure you don’t midify the plugin files so make a copy and paste into your theme files into a woocommerce folder) to the following (notice that I had removed my quantity input from my code so if you need it, ensure you rework the code to get it working):

    <form class="cart single-product" method="post" enctype='multipart/form-data'>
    	 	<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
    		<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
    		<button type="submit" class="single_add_to_cart_button button alt cart-buttton add-to-cart"><?php echo $product->single_add_to_cart_text(); ?></button>
    		<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
    	</form>
    
        <form class="cart single-product" method="post" enctype='multipart/form-data' action="/checkout?set-cart-qty_<?php echo $product->id;?>=1">
        	<button type="submit"  class="single_add_to_cart_button button alt cart-buttton buy-now">Buy Now</button>
            <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
        </form>

    I added another button next to the existing Add to Cart button but separating the form. The blog post mentions that you can add a hyperlink instead but the above worked for me in terms of the way I needed to customise the page (slightly more long winded)

    From blog:

    Usage instructions:

    Create a hyperlink with a query string argument like so:
    <url>?set-cart-qty_<product ID>=<quantity>
    Where <product ID> is the numerical ID of your product (something like “167”) and <quantity> is the >quantity you want set in the user’s shopping cart (most likely this will just be “1”).

    Example URL to send user to checkout with exactly one item of product with ID “167” in cart:

    https://my.website.com/checkout?set-cart-qty_167=1

    I hope the above helps anyone who has a similar problem as I had.

    I have implemented this as well and I added some error log statements, it looks like it goes into a continuous loop before the server kills the connection.

    [Wed Mar 04 21:02:52.272782 2015] [:error] [pid 27565] [client 71.181.125.154:60832] adding the product with quantity of 1
    [Wed Mar 04 21:02:55.957171 2015] [:error] [pid 27553] [client 71.181.125.154:60835] adding the product with quantity of 1, referer: https://jcrawford.heytix.com/?tribe_events=test-event-3
    [Wed Mar 04 21:02:58.297061 2015] [:error] [pid 27574] [client 71.181.125.154:60836] adding the product with quantity of 1, referer: https://jcrawford.heytix.com/?tribe_events=test-event-3
    [Wed Mar 04 21:03:00.726557 2015] [:error] [pid 27561] [client 71.181.125.154:60837] adding the product with quantity of 1, referer: https://jcrawford.heytix.com/?tribe_events=test-event-3
    [Wed Mar 04 21:03:03.019158 2015] [:error] [pid 27564] [client 71.181.125.154:60838] adding the product with quantity of 1, referer: https://jcrawford.heytix.com/?tribe_events=test-event-3
    [Wed Mar 04 21:03:05.180067 2015] [:error] [pid 27552] [client 71.181.125.154:60847] adding the product with quantity of 1, referer: https://jcrawford.heytix.com/?tribe_events=test-event-3

    For help with WooCommerce, I’d suggest starting your own thread on the forum specific to that plugin here:

    https://www.ads-software.com/support/plugin/woocommerce#postform

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Woocommerce – Add To Cart and Buy Now buttons on Product Pages’ is closed to new replies.