Skip to content

Instantly share code, notes, and snippets.

@yawalkar
Last active November 20, 2022 13:23
Show Gist options
  • Save yawalkar/4c83aa72932720863ec4 to your computer and use it in GitHub Desktop.
Save yawalkar/4c83aa72932720863ec4 to your computer and use it in GitHub Desktop.
WooCommerce - Login / Logout Cart Handling and redirects
<?php
/**
* Redirect users to custom URL based on their role after login and restore the cart
*
* @param string $redirect
* @param object $user
* @return string
*/
function wp_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
$homepage = home_url();
// redirect users based on cart count
$url = ( WC()->cart->get_cart_contents_count() !== 0 ) ? $checkout_url : $homepage;
$user_id = $user->ID;
$meta_key = 'cart-'.$user_id;
//$cart_content=get_user_meta($user_id, $meta_key, true);
$cart_content = get_option( $meta_key );
// add cart contents
foreach ( $cart_content as $cart_item_key => $values )
{
$id =$values['product_id'];
$quant=$values['quantity'];
$woocommerce->cart->add_to_cart( $id, $quant);
}
if( $role == 'administrator' || $role == 'shop-manager' || $role == 'author' || $role == 'editor' ) {
//Redirect administrator, shop-manager, author and editor to the "Dashboard" page
$redirect = $dashboard;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {
//Redirect customers and subscribers to the "My Account" page
$redirect = $url;
} else {
//Redirect any other role to the previous visited page or, if not available, to the home
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wp_custom_user_redirect', 10, 2 );
// Save the cart for the current user, empty cart and redirect user to home page
function logout_redirect(){
if( function_exists('WC') ){
global $woocommerce;
// get user details
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
$cart_contents = $woocommerce->cart->get_cart();
$meta_key = 'cart-'.$user_id;
$meta_value = $cart_contents;
//update_user_meta( $user_id, $meta_key, $meta_value);
update_option( $meta_key, $meta_value );
WC()->cart->empty_cart();
}
wp_safe_redirect( home_url() );
exit();
}
add_action('wp_logout','logout_redirect');
@iamharisali
Copy link

iamharisali commented Sep 7, 2017

Hi,
I want to redirect the user to cart page if he has something in cart I am using the following code but it doesn't seem to work

`add_filter( 'bp_login_redirect', 'bpdev_redirect_to_profile',50,3 );

function bpdev_redirect_to_profile( $redirect_to_calculated, $redirect_url_specified, $user ){

global $woocommerce;

if( sizeof($woocommerce->cart->cart_contents)>0  ){
 
$redirect_to_calculated="https://edkasa.com/checkout";
}
return $redirect_to_calculated;
}`

if I echo $redirect_to_calculated, it shows the correct URL i.e "https://edkasa.com/checkout" but it doesn't redirect to this URL
Please helpp!!

@AshishOdich
Copy link

AshishOdich commented Jul 25, 2018

Hiii @haris4090 use following code.
`function woocommerce_custom_redirects() {

// Case1: Non logged user on checkout page (cart empty or not empty)
if ( !is_user_logged_in() && is_checkout() )
    wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );

// Case2: Logged user on my account page with something in cart
if( is_user_logged_in() && !WC()->cart->is_empty() && is_account_page() )
    wp_redirect( get_permalink( get_option('woocommerce_checkout_page_id') ) );

}
add_action('template_redirect', 'woocommerce_custom_redirects');`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment