Woocommerce - Add to Cart and Buy Now Buttons on Product Pages

I am trying to add a buy now button in Woocommerce on the product page, so there are two buttons:

  • Add to Shopping Cart
  • buy now (which will add the product to the cart and redirect to checkout)

I still want to add to cart to work fine.

How can I achieve this? Many thanks.

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

+3


source to share


3 answers


I managed to solve this problem by finding this blog post http://samplacette.com/skip-shopping-cart-in-woocommerce/ .

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

I copied the following text into my theme's 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 changed the theme / woocommerce / single -product / add-to-cart / simple.php (make sure you don't flatten your plugin files, so make a copy and paste in your theme files in your woocommerce folder) to the following (note, that I removed my quantity input from my code, so if you need it make sure you rewrite the code to make it work):



<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 splitting the form. The blog post mentions that you can add a hyperlink, but the above worked for me in terms of how I needed to set up the page (a little longer winding).

From the blog:

Instructions for use:

Create a hyperlink with a query string argument like this: Set cart-qty_ = Where is the numerical ID of your product (something like "167") and the quantity you want to set in the user's shopping cart (most likely it will just be "1").

Example URL to submit a user for review with just one product item with ID "167" in their cart:

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

I hope the above helps anyone who has a similar problem like mine.

+4


source


After a lot of searching, I was surprised that this is not a standard. Here is my solution:

Or use a hook like "woocommerce_single_product_summary"

Or copy wp-content / plugins / woocommerce / templates / single-product / add-to-cart / simple.php to your child theme, for example: wp-content / themes / children Theme / WooCommerce / mono-product / add to cart / simple .php

Edit the file and add the following code wherever you want:

<div class="express-checkout-wrapper">
            <a id="dc_express_checkout" href="/checkout/?add-to-cart=<?php echo get_the_ID(); ?>">
                Purchase
            </a>
        </div>

      

Now the only problem is that the button will take you out for validation and add the correct product but without the correct quantity if you changed it, so I used js in my custom.js file, which is queued in the footer:



// Add and change quantity to express checkout button when the quantity is updated
if($('.cart .qty').length){
    var originalUrl = $('#dc_express_checkout').attr('href');
    $('.cart .qty').change(function(){
        var url = originalUrl + '&quantity=' + $(this).val();
        $('#dc_express_checkout').attr('href', url);
    });
}

      

You can change the url:

href="/checkout/?add-to-cart=<?php echo get_the_ID(); ?>"

      

in

href="/cart/?add-to-cart=<?php echo get_the_ID(); ?>"

      

If you want the button to go to the cart instead of the checkout page.

0


source


How do I add a Buy Now button to variable products?

0


source







All Articles