I'm trying to add a reCAPTCHA to WooCommerce Checkout on WordPress. When I use the following snippet the captcha loads and works correctly:
add_action( 'woocommerce_review_order_before_payment', 'woocommerce_checkout_recaptcha_field' );
function woocommerce_checkout_recaptcha_field( ) {
echo '<div class="g-recaptcha" data-sitekey="XXXXXXXXXXXX"></div>';
}
However as soon as I move it next to the terms and conditions checkbox:
add_action( 'woocommerce_checkout_before_terms_and_conditions', 'woocommerce_checkout_recaptcha_field' );
function woocommerce_checkout_recaptcha_field( ) {
echo '<div class="g-recaptcha" data-sitekey="XXXXXXXXXXXX"></div>';
}
The captcha stops loading. I can see the captcha beginning to load as the page makes a gap for it but then after a split second it disappears again.
Anyone got any ideas?
Thanks
Related
I have a WooCOmmerce Login Page which I am customizing at the moment, by default the login and registration page are in one page. I had seperated the login and registration pages into 2 seperate forms right now. Currently I would like to put a link for the seperated registration page right below the "Forgot Password" link.
But my output is as follows :
I would like for it to appear as follows:
Below is the action hook I am using for my login page.
add_action( 'woocommerce_after_customer_login_form', 'register_link' );
function register_link() {
?>
<div class="woocommerce-register-link">
<p><?php _e( 'Register a new account here' ); ?></p>
</div>
<?php
}
Any help would be much appreciated.
This can be done in multiple ways:
You can edit the template myaccount/form-login.php to insert your code in the right location.
you can use Javascript/jQuery to insert your code in the right location, without editing any template:
// The jQuery Ajax request
add_action( 'wp_footer', 'wc_separated_login_form_js' );
function wc_separated_login_form_js() {
// Only my account for unlogged users
if( ! is_user_logged_in() && is_account_page() &&
'yes' === get_option('woocommerce_enable_myaccount_registration') ):
// Your button html to insert
$html = sprintf(
'<p class="woocommerce-Registerlink register-link">%s</p>',
home_url('/registration/'), // <== Here set your registration link
__( 'Register a new account here')
);
// jQuery
?>
<script type="text/javascript">
jQuery( function($){
$('p.lost_password').after('<?php echo $html; ?>');
});
</script>
<?php
endif;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
I'm trying to add additional text woocommerce_before_checkout_form in functions.php via Editor but I see following error:
"Your PHP code changes were rolled back due to an error on line 0 of
file Unknown. Please fix and try saving again. Exception thrown
without a stack frame"
function bonus() {
echo "<div id='bonus'>Don't you have a coupon?Click here to get 5% OFF</div>";
}
add_action( 'woocommerce_before_checkout_form', 'bonus' );
When I add only text without ahref everything works.
I want to add to this ahref redirect to: https://mywebsite.com/checkout/?apply_coupon=promo5
Try this.
You need to change <a href="https://mywebsite.com/checkout/?apply_coupon=promo5?apply_coupon=promo5"> to <a href='https://mywebsite.com/checkout/?apply_coupon=promo5?apply_coupon=promo5'>
function bonus() {
echo "<div id='bonus'>Don\'t you have a coupon?<a href='https://mywebsite.com/checkout/?apply_coupon=promo5?apply_coupon=promo5'>Click here to get 5% OFF</a></div>";
}
add_action( 'woocommerce_before_checkout_form', 'bonus' );
You have used double quote within double quote that is the reason error is coming.
Try below code it will work.
function bonus() {
echo "<div id='bonus'>Don't you have a coupon?<a href='https://mywebsite.com/checkout/?apply_coupon=promo5?apply_coupon=promo5'>Click here to get 5% OFF</a></div>";
}
add_action( 'woocommerce_before_checkout_form', 'bonus' );
I am trying to create a coupon click counter.
I received the default number of clicks using the code
<?php $couponusers = get_field('coupon_users'); ?>
And I displayed the default number of clicks using the code,
<p><?php echo $couponusers ?></p>
Now, I want to increase the value of ‘coupon_users’ whenever a click is made. Any suggestions on how to do this?
The page I need help with: https://deals.onlinerockershub.com/bluehost-web-hosting/
Add this action to your functions.php
function coupon_callback() {
//
// Add here cookie check
//
if(is_page('1')){ // ID of "Thank you for coupon click" page
update_field('coupon_users',$couponusers); // or similar
}
return;
}
add_filter( 'wp', 'coupon_callback' );
I've encountered a weird thing in WP WooCommerce and I can't understand nor fix it myself.
Thing is I've added a clear button in my checkout page,
that button redirects me to my homepage and adds a ?clear param.
Then I check if that param is set and if it is set then the cart is cleared;
Code:
if(isset($_POST["clear_cart"]))
{
header("Location: https://examplepage.com?clear");
}
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url()
{
global $woocommerce;
if(isset( $_GET['clear']))
{
$woocommerce->cart->empty_cart();
}
}
Now the bug/error thing.
This code works... Once. I'll do my best to explain now.
When I click the clear button for the first time - it works.
I'm being redirected to my homepage, cart is cleared, everything works.
When I go and add some products again and then I clear the cart
I get redirected to my homepage again (first 4 lines of code)
but my cart isn't cleared now. To get it cleared I have to change my param to something like
?clear=true
Then I do the same thing and after clicking clear cart button it redirects me again and the cart isn't cleared. If I again change the param to
?clear=true
it doesn't work this time - because it worked before. Changing the param to
?clear=true1
clears the cart.
I hope you've already understand what I'm talking about.
I've tried various params instead of "clear" and everytime the same thing happens.
When I also tried echoing something within
function woocommerce_clear_cart_url()
it also worked only once. I'm out of ideas.
Thank you.
Try below code
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if(isset($_POST["clear_cart"]))
{
$data= $woocommerce->cart->empty_cart();
if(is_null($data) == 1 ){
wp_redirect( site_url() );
exit;
}
}
Assuming your html code is something like below :
<form method="post">
<input type="submit" name="clear_cart" value="1">
</form>
You need to try this code for your error.
add_action("template_redirect", 'e_coding_hub_redirection_function');
function e_coding_hub_redirection_function(){
global $woocommerce;
if( is_cart() && WC()->cart->cart_contents_count == 0){
//wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
wp_redirect( site_url() );
}
}
I am running a wholesale shop on Woocommerce. Login is required to see the prices. This is set up and working properly. Now I wish to add a logon form on every product page to only show to visitors (not logged on users).
I am using the WooCommerce Catalog Visibility plugin. This plugin offers the functionality I described above, but my theme is somehow messing it up. The plugin author says to talk to the theme developer and the theme developer says to talk to the plugin author. So now I am trying to find a workaround.
First issue: The plugin comes with a shortcode [woocommerce_logon_form] that will display a logon form. I don't want to manually add this to every existing product since I have thousands of products on my site. I am looking for a way to get it in through the code for the product page layout.
I found this code (to be added to the functions.php) to work well:
// adds notice at single product page above add to cart
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<p id="rtrn">30-day return policy offered. See Terms and Conditions for details.</p>';
}
However, it will only show text. The short code won't work when added instead of the sample text.
Second issue: The short code shows the form even when the customer is already logged in.
I am currently using this nice code that shows or hides content depending on whether the user is logged in or not:
add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
That shortcode works perfectly for text, but not with other shortcodes.
So the combination of these short codes: [visitor][woocommerce_logon_form][/visitor] will not show the logon form to visitors. Instead it will only show them this as text [woocommerce_logon_form].
Please help! I am sure this is probably easily fixed by someone with coding skills.
I appreciate your effort to answer to this question. Keep in mind that my understanding of code is very limited and it would be great if you can also point out in which file to add or modify code.
To make your shortcode working in php code or in php/html code you need to use a native WordPress function do_shortcode() … You can use it with your shortcode for example in your 1st function this way:
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo do_shortcode('[woocommerce_logon_form]');
}
And this will work…
To see all the different hooks you can use instead of woocommerce_single_product_summary, please see this 2 templates code to chose in case a more convenient hook:
WooCommerce single-product.php template
WooCommerce content-single-product.php template
You can also add it the same way in one of your existing short codes, this way:
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return do_shortcode('[woocommerce_logon_form]');
return '';
}
And this will work too.
See as reference this answer: Change markup in WooCommerce shortcode output
So as you can see your problem is solved on both issues