How to easily add WooCommerce One-Click Checkout for WordPress without using a plugin.

Make your website easier and fast to use by allowing fast checkout. In this tutorial will are going to add few lines of code using PHP. Let see How to easily add WooCommerce One-Click Checkout for WordPress without using a plugin.

How is it done?

  • Install and activate the plugin. After the activation, scroll to your WordPress menu and you will find a button for snippets, and on hover, three options will pop up.
  • Give your snippets a name.
  • Paste your snippets function code
  • save and activate your code.

How do you do it?

Watch Attached YouTube tutorial for more Guidance

/**
 * Skip cart and redirect to direct checkout
 * 
 * @Version 1.0.1
 * @mail-contact@thiarara.co.ke
 * @https://thiarara.co.ke
  * @Thiarara
 */

// Skip the cart and redirect to check out url when clicking on Add to cart
add_filter ( 'add_to_cart_redirect', 'redirect_to_checkout' );
function redirect_to_checkout() {
    
	global $woocommerce;

	// Remove the default `Added to cart` message
	wc_clear_notices();

	return $woocommerce->cart->get_checkout_url();
	
}

// Global redirect to check out when hitting cart page
add_action( 'template_redirect', 'redirect_to_checkout_if_cart' );
function redirect_to_checkout_if_cart() {
	
	if ( !is_cart() ) return;

	global $woocommerce;

	if ( $woocommerce->cart->is_empty() ) {
		// If empty cart redirect to home
		wp_redirect( get_home_url(), 302 );
	} else {
		// Else redirect to check out url
		wp_redirect( $woocommerce->cart->get_checkout_url(), 302 );
	}
	
	exit;
}

// Empty cart each time you click on add cart to avoid multiple element selected
add_action( 'woocommerce_add_cart_item_data', 'clear_cart', 0 );
function clear_cart () {
	global $woocommerce;
	$woocommerce->cart->empty_cart();
}

// Edit default add_to_cart button text (single product)
add_filter( 'BUY NOW', 'woo_custom_cart_button_text' );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_cart_button_text' );
function custom_cart_button_text() {
	return __( 'Buy Now', 'woocommerce' );
}
// To change add to cart text on product archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );  
function woocommerce_custom_product_add_to_cart_text() {
    return __( 'Buy Now', 'woocommerce' );
}

// Unset all options related to the cart
update_option( 'woocommerce_cart_redirect_after_add', 'no' );
update_option( 'woocommerce_enable_ajax_add_to_cart', 'no' );

CSS code you will need to add to hide view cart:

NB: Adding CSS will be determined with the theme you are using:

  • On the dashboard Menu,
  • Select appearance and hover,
  • Select Customize
  • Selec Add CSS And paste the CSS Code Provided.

Additional CSS code If needed.

/*** Hide view cart ***/
.woocommerce.widget_shopping_cart .buttons a:not(.checkout), .woocommerce .widget_shopping_cart .buttons a:not(.checkout) {
    background: transparent;
    color: var(--global-palette4);
    box-shadow: none;
    display: none;
}

With that you will be able to add add Woocommerce One-Click Checkout for WordPress without using a plugin.

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on whatsapp
Share on telegram

Leave a Reply