Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abid112/c653f3b035af054ad269b862b0cbe139 to your computer and use it in GitHub Desktop.
Save abid112/c653f3b035af054ad269b862b0cbe139 to your computer and use it in GitHub Desktop.
This function is provide the facility of another button called "Direct Checkout" besides 'Add to Cart" which will takes user to the direct checkout page on Woocommerce.
/********************************************************************************************
*Add this on functions.php of your WordPress
*********************************************************************************************/
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' ) )
?>
<script>
jQuery(function($) {
// if our custom button is clicked
$(".custom-checkout-btn").on("click", function() {
// get the value of the "href" attribute
$(this).attr("href", function() {
// return the "href" value + the string "&quantity=" + the current selected quantity number
return this.href + '&quantity=' + $('input.qty').val();
});
});
});
</script>
<?php
{
echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="single_add_to_cart_button button custom-checkout-btn alt">Buy Instant</a>';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment