Add Captcha In Woocommerece Checkout Page

Add Captcha In Woocommerece Checkout Page

In this article we will show you how to add the captcha in checkout page woocommerece.

Step 1: Using WordPress ReCaptcha Integration Plugin

Install and active WordPress ReCaptcha Integration Plugin

Step 2: Copy and paste this code in your function.php

// Add [recaptcha] shortcode
if ( class_exists('WP_reCaptcha') )
	add_shortcode( 'recaptcha' , array( WP_reCaptcha::instance() , 'recaptcha_html' ) );


// Customizing checkout fields using actions and filters
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {
	if ( ! is_user_logged_in() ) :
	    echo '<div id="my_captcha"><label>' . __('Captcha') . '</label>';

	    	echo do_shortcode('[recaptcha]');

	    echo '</div>';
    endif;
}

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

// Valided error
function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( !WP_reCaptcha::instance()->recaptcha_check() )
        wc_add_notice( __( 'Please enter correct captcha.' ), 'error' );
}

Check your check out page, Captcha will display.

Comments

comments

Share