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() );
}
}
Related
I'm using WordPress with Elementor, I want a certain page to be accessible only if it comes from a certain url. I saw from other answers in similar questions that I can use this:
add_action( 'template_redirect', 'wpse15677455_redirect' );
function wpse15677455_redirect() {
$value = ('https://mywebsite.com/quotaton/') ;
if (!is_page(555) & wp_get_referer() !== $value ) {
wp_safe_redirect( get_home_url() );
}
};
I tried using this in the function.php of the theme but it returns the error "Unable to communicate with server to check for fatal errors". I tried with all plugins deactivated except elementor but same result.
I tried without the add_action call but, despite not giving errors, it also does nothing. I can't seem to find the right place/way to use this function.
Trying out the code, I believe the problem is that you're missing a single ampersand(&) for the And operator. Also, if is_page is used to check for the "certain page", maybe the not(!) operator isn't necessary...
if (is_page(555) && wp_get_referer() !== $value ) {
function custom_redirects() {
if ( is_front_page() ) {
wp_redirect( home_url( '/dashboard/' ) );
die;
}
if ( is_page('contact') ) {
wp_redirect( home_url( '/new-contact/' ) );
die;
}
}
add_action( 'template_redirect', 'custom_redirects' );
is_page('contact') => 'contact' is page slug.
is_page(150) => '150' is page ID.
If the link that has to be redirected comes from a page, you could use a redirection plugin like this one: https://it.wordpress.org/plugins/page-links-to/
Once activated you just edit the page you want to be redirected inserting the correct link in the page links to field.
I try to add a wc notice in an admin page.
function some_hook_function() {
wc_add_notice(__('Done!'), 'notice');
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'edit.php?post_type=shop_order' ) );
exit;
}
add_action( 'some_action' , 'some_hook_function', 10);
However it doesn't work. I am able to add an admin notice but I don't want to do that. I just need a classical wc notice after a redirection.
You have to active cookie/session for the customer by following code before calling wc_add_notice function:
if (!WC()->session->has_session()) {
WC()->session->set_customer_session_cookie(true);
}
It's necessary for actions such as payment response to your website.
You can't display it on admin pages without calling wc_print_notices().
By default this function will echo all the notice. If however you want to get the notices, pass true in the function as $notices = wc_print_notices(true);
I'm want to clear all items in cart when a new session is started. I've tried
add_action( 'init', 'clear_cart_on_it' );
function clear_cart_on_it() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
It is throwing this error:
Fatal error: Call to a member function empty_cart() on null in /home/shuggapa/public_html/wp-content/plugins/candy-scoops/scoops.php on line 53
I have no idea why. Please how can I implement this.
Adding to #"Vidish Purohit"'s answer, you want to additionally confirm the referer, and use a more suitable hook.
On the page initiating the delete, use:
<form method="post">
<input type="hidden" name="action" value="empty_cart">
<?php wp_nonce_field("empty_cart", "empty_cart"); ?>
<button type="submit">Remove all</button>
</form>
To handle the delete, add this hook in your functions:
add_action('woocommerce_init', 'my_woocommerce_init');
function my_woocommerce_init() {
if (isset($_POST["action"]) && $_POST["action"] === "empty_cart") {
wp_verify_nonce("empty_cart", "empty_cart");
WC()->cart->empty_cart();
}
}
First it checks that the submit happened ($_POST['action'] present and matching), then wp_verify_nonce checked that the POST really was initiated from a reliable origin.
And most essentially, the woocommerce_init hook makes sure the global WC() returns something to work with.
In your code, init hook is triggered before Woocommerce object is instantiated. You can use this hook:
add_action( 'template_redirect', 'clear_cart_on_it' );
or
add_action( 'wp_loaded', 'clear_cart_on_it' );
I want to display the header cart (located on the menu) only if the user is logged in. Here's what I got so far:
add_action('init','remove_header_cart_if_user_not_logged_in');
function remove_header_cart_if_user_not_logged_in() {
if (is_user_logged_in()) {
return;
} else {
add_action( 'init', 'woa_remove_header_cart' );
function woa_remove_header_cart() {
remove_action( 'storefront_header', 'storefront_header_cart', 60 );
}
}
This code creates an error and prevents my website to display. " The [domain] page isn’t working [domain] is currently unable to handle this request.
HTTP ERROR 500"
The else part alone (woa_remove_header_cart) works well, but when I try to put it inside of the "if user logged in" condition, it generates the error.
What if you simplified it to:
add_action( 'storefront_header','remove_header_cart_if_user_not_logged_in' );
function remove_header_cart_if_user_not_logged_in() {
if ( ! is_user_logged_in() ) {
remove_action( 'storefront_header', 'storefront_header_cart', 60 );
}
}
You are adding two functions to the init hook at the same time/priority? It's weird at best, and may be causing your error. I'm also not sure if WP knows the user is logged in by init. Don't have time to check now, but you can avoid it. You don't have to remove a function on the init hook, you just have to do it before the function is executed. In my example, I'm using the storefront_header hook, but since the default (10) priority is lower than 60 it should work.
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