Last active
August 4, 2016 09:43
-
-
Save mikakaltoft/91ae844fb91cb284dd888d45d5a225d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Dynamic WooCommerce Login | |
Description: This plugin enables a dynamic WooCommerce login feature. If the user is not logged in upon checkout, they are prompted to login or to register an account on the my-account page beforhand. If they have products in their cart they will be redirected to the checkout-page. If not, they will be redirected to the homepage. | |
Plugin URI: http://rocapress.com | |
Author: RocaPress | |
Author URI: http://rocapress.com | |
Version: 1.0 | |
License: GPL2 | |
*/ | |
add_action( 'template_redirect', 'dlw_non_logged_in_users' ); | |
function dlw_non_logged_in_users() { | |
if ( is_checkout() && ! is_user_logged_in() ) { | |
wp_redirect( site_url() . '/my-account', 301 ); | |
exit; | |
} | |
} | |
add_filter('woocommerce_login_redirect', 'dlw_login_redirect'); | |
function dlw_login_redirect( $redirect_to ) { | |
global $woocommerce; | |
if ( WC()->cart->get_cart_contents_count() == 0 ) { | |
$redirect_to = home_url(); | |
} else { | |
$redirect_to = $woocommerce->cart->get_checkout_url(); | |
} | |
return $redirect_to; | |
} | |
add_filter('woocommerce_registration_redirect', 'dlw_wc_registration_redirect'); | |
function dlw_wc_registration_redirect( $redirect_to ) { | |
global $woocommerce; | |
if ( WC()->cart->get_cart_contents_count() == 0 ) { | |
$redirect_to = home_url(); | |
} else { | |
$redirect_to = $woocommerce->cart->get_checkout_url(); | |
} | |
return $redirect_to; | |
} | |
add_action('wp_logout',create_function('','wp_redirect(home_url());exit();')); | |
/* To make it work, simply make sure that [woocommerce_my_account] is added to your my-account page and that registration also is enabled on the "My Account" page in the WooCommerce settings. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment