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);
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 am using the plugin force login: https://en-gb.wordpress.org/plugins/wp-force-login/ and need to allow guests to get to the order received page after purchasing.
After checking out, a user which is logged in would be forwarded to this page: [mydomain]/checkout/order-received/[order_id]/?key=[order_key]. I have tried this: Show customer details on WooCommerce Thankyou page even if not registered but could not figure out what to do after I added it.
I currently have this code which allows certain pages to be whitelisted so users that are not logged in can bypass the "force-login" plugin and pay for the relevant product:
add_filter('v_forcelogin_whitelist', 'my_forcelogin_whitelist', 10, 1);
function my_forcelogin_whitelist() {
return array(
home_url( '/cart/' ),
home_url( '/checkout/' ),
home_url( '/cart/?add-to-cart=1465' ),
);
}
I want none logged in users to be forwarded to a page which looks like this after checkout:
[mydomain]/checkout/order-received/5304/?key=wc_order_5cffcfbc96028
For anyone that has this problem this is how I got it working. Since some of the URL's generated are dynamic I needed a work around for those. Using the following code in function.php works for ALL URL's assiciated with woocommerce:
function my_forcelogin_bypass( $bypass ) {
if ( class_exists( 'WooCommerce' ) ) {
if ( is_woocommerce() || is_wc_endpoint_url() ) {
$bypass = true;
}
}
return $bypass;
}
add_filter( 'v_forcelogin_bypass', 'my_forcelogin_bypass' );
WooCommerce Checkout/order-received issue
For the problem of [mydomain]/checkout/order-received/[order_id]/?key=[order_key] it is not loading right or is not showing something or 500 Internal Server Error ?
For Temporary purpose because whenever the plugin will be updated the file will be updated in a woo-commerce plugin?
Open File Zilla
The Visit : /var/www/html/wp-content/plugins/woocommerce/includes directory
Then in the directory open : class-wc-order.php
Find this with ctrl+F : get_checkout_order_received_url()
There Will be two lines of code(Earlier) :
$order_received_url = wc_get_endpoint_url( 'order-received', $this->get_id(), wc_get_checkout_url() );
$order_received_url = add_query_arg( 'key', $this->get_order_key(), $order_received_url );
Change to(Updated) Add a comment in the second line :
$order_received_url = wc_get_endpoint_url( 'order-received', $this->get_id(), wc_get_checkout_url() );
//$order_received_url = add_query_arg( 'key', $this->get_order_key(), $order_received_url );
Save it and update it to the server.
You issue will be resolved, but it's for temporary it will be changes whenever the woocommerce plugin will be updated, so you have to update it again.
Thanks!
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 understand that I can use the following code in WordPress to ensure that users who are already logged into my site are not re-directed to the Dashboard when they click the login button on my home page:
<?php
if(is_user_logged_in()){
// redirect to desired page
}
?>
I would be grateful for advice on where I should place this code and which part of it exactly should be overwritten with the URL for the page to which I wish to send logged in users. I should add that the desired page is the same page as I direct users to on logging in.
Many thanks in advance for your kind assistance.
Just take a look here, for more perspective: https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
If you want to land your Users, for example to this page example.com/some-page/ after login, you can use this code:
function mysite_login_redirect($redirect_to, $request, $user) {
if (!isset($_GET['loggedout'])) {
return (isset($user->roles) && is_array($user->roles) && in_array('administrator', $user->roles)) ? admin_url() : site_url('/some-page/');
}
}
add_filter('login_redirect', 'mysite_login_redirect', 10, 3);
The algorithm for doing this is simple:
If the user is an administrator, continue to admin_url()
Otherwise, redirect to, say the site_url('/some-page/')
The login_redirect filter will pass three arguments to the function:
$redirect_to
$request
$user
We’re primarily concerned with the third as we can take a look at its roles attribute to determine if it contains the administrator value.
And with that, we are now redirecting non-admin users to the specific page rather then the dashboard.
site_url('/some-page/') will transform to example.com/some-page/
Hope it will help you :)
As I undestood about your question, you are trying to prevent users to visualize the dashboard of WordPress. You can prevent non-admin users to go to the Dashboard on the admin init hook.
add_action( "admin_init", "prevent_dashboard_non_admin" );
function prevent_dashboard_non_admin() {
if ( ! current_user_can('manage_options') && ! defined( 'DOING_AJAX' ) ) {
wp_redirect( home_url() ); // You can change that home_url() to any page URL you are tring to redirect
}
}
You can put this code inside functions.php
Note: This code has not been tested, please leave a comment and I will create the update if you have any issues with it.
UPDATE
function my_is_loggin_page() {
if ( $GLOBALS['pagenow'] === 'wp-login.php' ) {
if( is_user_logged_in() ) {
wp_redirect( "yourpage" );
}
}
}
add_action( "init", "my_is_loggin_page", 12 );
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.