Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce - Add To Cart and Buy Now buttons on Product Pages

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.

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

like image 974
Fiz S Avatar asked Nov 18 '14 00:11

Fiz S


1 Answers

I managed to resolve this by finding this blog post http://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: ?set-cart-qty_= Where is the numerical ID of your product (something like “167”) and 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:

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

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

like image 179
Fiz S Avatar answered Oct 18 '22 03:10

Fiz S