In WooCommerce, I'm trying to find a way to disable the woocommerce checkout page for non logged users, OR when they try to checkout they get redirected to the log in page.
So they should log in first to be able to continue their checkout.
Is that possible?
Thanks
Is possible to redirect non logged customers that trying to access to checkout with this code:
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
// Here the conditions (woocommerce checkout page and unlogged user)
if( is_checkout() && !is_user_logged_in()){
// Redirecting to your custom login area
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
// always use exit after wp_redirect() function.
exit;
}
}
Then you can display a custom notice in cart page with a button linked to login area, to avoid customer frustration. Is better to warn customer before, than after.
// Displaying a message on cart page for non logged users (Optional)
add_action( 'woocommerce_before_cart', 'customer_redirected_displaying_message');
function customer_redirected_displaying_message() {
if( !is_user_logged_in() ){
// HERE Type your displayed message and text button
$message = __('To access checkout, you need first to be logged in', 'woocommerce');
$button_text = __('Login area', 'woocommerce');
$cart_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
wc_add_notice( $message . '' . $button_text . '', 'notice' );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works.
Related
I am trying for solution for woocommerce to check if the user is logged in when they click on the checkout button and not take to checkout page untill logedin or register . If the user is not logged in, open login register form of xootix plugin using its class .xoo-el-login-tgr, which shows a login and registration form. If the user logs in with existing credentials and there are products in the cart, they will be redirected to the checkout page. If there are no products in the cart, they will remain on the same page after login. If the user registers as a new user and there are products in the cart, they will be redirected to the checkout page. If there are no products in the cart, they will remain on the same page after registration.
I am using custmized code to achive this but it is not working when i click on checout button it is taking into checkout page as the code not at all applies.Looking for help.
I am trying below cusmized code to achive this
add_action( 'wp_footer', 'redirect_to_login_on_checkout' );
function redirect_to_login_on_checkout() {
if ( ! is_user_logged_in() && is_checkout() ) {
?>
<script>
jQuery(document).ready(function($) {
$('.checkout-button').on('click', function(e) {
e.preventDefault();
$('.xoo-el-login-tgr').trigger('click');
});
});
</script>
<?php
}
}
add_action( 'wp_login', 'redirect_to_checkout_on_login', 10, 2 );
function redirect_to_checkout_on_login( $user_login, $user ) {
if ( is_checkout() && ! WC()->cart->is_empty() ) {
wp_safe_redirect( wc_get_checkout_url() );
exit;
}
}
add_action( 'user_register', 'redirect_to_checkout_on_registration' );
function redirect_to_checkout_on_registration( $user_id ) {
if ( is_checkout() && ! WC()->cart->is_empty() ) {
wp_safe_redirect( wc_get_checkout_url() );
exit;
}
}
The solution i am looking is if some one add products to cart checkout button appera(cart page disabled)on clickng on checkout check used loged in or not ,if he login take to the checkout page if not loged in present login registrion form using XOOTIX plugin class to allow user to login or register.If new user rgister adn if there are products in the cart take to checkout page.
I trying to make all in one page for my checkout.
My issue is in checkout page when clicking on place order button, a redirect to payment page will happen.
is there a way to disable the redirect after placing the order successfully, to avoid moving to the next page?
you can change redirect page by using the below code..
add_action( 'template_redirect', 'woo_order_received_redirection_to_my_account' );
function woo_order_received_redirection_to_my_account() {
// Only on "Order received" page
if( is_wc_endpoint_url('order-received') ) {
global $wp;
// Get the Order Object
$order = wc_get_order( absint($wp->query_vars['order-received']) );
// My account redirection url
$my_redirect_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
// if you want to redirect cart page...
// $my_redirect_url = home_url( 'checkout' );
wp_redirect( $my_redirect_url );
exit(); // Always exit
}
}
Following the woocommerce documentation, I added an endpoit to my-account page in woocommerce.
I want to make this endpoint visible only to a specific user role, lets say shop_manager.
Is there a way to redirect to a 404 page users who try to access directly that endpoint?
Thanks in advance.
Assuming that you have already created a custom endpoint to my account section (see this related answer), you can redirect all non allowed user roles to a specific page using template_redirect hook in this simple way:
add_action( 'template_redirect', 'custom_endpoint_redirection' );
function custom_endpoint_redirection() {
$allowed_user_role = 'administrator';
$custom_endpoint = 'my-custom-endpoint';
if ( is_wc_endpoint_url($custom_endpoint) && ! current_user_can($allowed_user_role) ) {
$redirection_url = home_url( '/404/' );
wp_redirect( $redirection_url );
exit;
}
}
You need to specify your custom end point, your allowed user role and the url redirection.
Code goes in functions.php file of your active child theme (or active theme). It could works.
Related:
WooCommerce - Assign endpoints to multiple custom templates in my-account page
WooCommerce: Assigning an endpoint to a custom template in my account pages
WooCommerce: Adding custom template to customer account pages
Custom my account new menu item for a specific user role in Woocommerce
Just add the follows code snippet in your active theme's functions.php and this is only for administrator user role, you can change it as per you -
function add_custom_my_account_endpoint() {
add_rewrite_endpoint( 'shop_manager', EP_PAGES );
}
add_action( 'init', 'add_custom_my_account_endpoint' );
function add_custom_wc_menu_items( $items ) {
$user = wp_get_current_user();
if( $user && in_array( 'administrator', $user->roles ) ){
$items[ 'shop_manager' ] = __( 'Shop Manager', 'text-domain' );
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_custom_wc_menu_items' );
function add_shop_manager_endpoint_content(){
$user = wp_get_current_user();
if( $user && !in_array( 'administrator', $user->roles ) ) return;
echo "Your content goes here";
}
add_action( 'woocommerce_account_shop_manager_endpoint', 'add_shop_manager_endpoint_content' );
After this just flush_rewrite_rules from Backend Settings > Permalinks. Thats it.
In my WooCommerce shop, when a customers is not logged in I would like to avoid him adding to cart asking him to either login or register an account…
Is it possible?
Updated - You could use this 2 little snippets of code, that will:
Replace add-to-cart button/link in shop pages and archives pages (for non logged in users).
Avoid non logged in customers adding to cart and displaying a custom message.
Here is the code:
// Replacing add-to-cart button in shop pages and archives pages (forn non logged in users)
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
function conditionally_change_loop_add_to_cart_link( $html, $product ) {
if ( ! is_user_logged_in() ) {
$link = get_permalink($product_id);
$button_text = __( "View product", "woocommerce" );
$html = ''.$button_text.'';
}
return $html;
}
// Avoid add to cart for non logged user (or not registered)
add_filter( 'woocommerce_add_to_cart_validation', 'logged_in_customers_validation', 10, 3 );
function logged_in_customers_validation( $passed, $product_id, $quantity) {
if( ! is_user_logged_in() ) {
$passed = false;
// Displaying a custom message
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
wc_add_notice( $message, 'error' );
}
return $passed;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested ad works.
I'm trying to redirect people who click on the add to cart button to the checkout page directly. The add to cart button only is adding the product to cart but not redirecting at all.
The code I use is only working for product with no variations.
I'm using the latest version of woocommerce 3.0.1.
//REDIRECT TO CHECKOUT
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
return WC()->cart->get_checkout_url();
}
Try with this:
function add_to_cart_checkout_redirect() {
wp_safe_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
die();
}
add_action( 'woocommerce_add_to_cart', 'add_to_cart_checkout_redirect', 11 );