We've been building out a new digital dashboard for my client's upcoming launch of a new digital product, however we wish to only provide access to this newly skinned and structured WooCommerce dashboard (my-account template pages) IF the user has been manually provided a user-role, ie: beta_user, org_manager
Any users who do not have this custom role applied would be served the default WooCommerce plugin my-account pages.
Here is what we have tried...
Added this snippet to the Child Theme functions.php:
// allow access to new dashboard if user role is org manager
function isOrgManager(){
$currentUser = wp_get_current_user();
return in_array('org_manager', $currentUser->roles);
}
Then applied this to the /child-theme/woocommerce/my-account.php file:
<?php if( isOrgManager() ){
wc_get_template( '/wp-content/themes/blankchildhub/woocommerce/my-account.php' );
} else {
wc_get_template( 'myaccount/my-account.php' );
}?>
Any insight would be greatly appreciated as I know this is a fairly common challenge for custom WP and WooCommerce managers.
Thank you!
UPDATED RESPONSE TO JSON:
Hi Json, thank you for taking an in-depth look at this. I've modified the child theme template and was able to restrict the displayed content based on the user role. Tested this between two different logins.
A) I found that having multiple roles assigned to users created an issue where a user with Primary role as 'Administrator' with a secondary role set as 'Org Manager' would cause the blank content to appear. So I've modified it accordingly...
$user = wp_get_current_user();
$allowed_roles = array('org_manager', 'administrator');
<?php if( array_intersect($allowed_roles, $user->roles ) ) { ?>
// Stuff here for allowed roles
<?php } ?>
B) I was a bit confused with your first recommendation here:
"First remove the old file /child-theme/woocommerce/my-account.php
and then copy /plugins/woocommerce/templates/my-account/my-account.php
to /child-theme/woocommerce/my-account.php".
In order to serve the default plugin account version (without our custom CSS and layout), I'm assuming this should be applied to the functions.php file, then filter users based on if the approved role is applied conditionally to either the custom theme template or the default. Would that make the most sense?
In other words, if User A has "Org Manager" then they will be served the child template files (/child-theme/woocommerce/myaccount/my-account.php) - else if User B does not have "Org Manager" in the roles array, they will be displayed the wc_get_template ( '/plugins/woocommerce/templates/my-account/my-account.php' ).
Does this seem like the best way to implement it since we have all of the WC template files copied and customized within the child theme?
Screenshots attached
Many thanks!
CodePudding user response:
First remove the old file /child-theme/woocommerce/my-account.php
and then copy /plugins/woocommerce/templates/my-account/my-account.php
to /child-theme/woocommerce/my-account.php
then add a switch/ if statement something like below:
<?php
/*** filename /child-theme/woocommerce/my-account.php ***/
/**
* My Account page
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/my-account.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.5.0
*/
defined( 'ABSPATH' ) || exit;
/**
* My Account navigation.
*
* @since 2.6.0
*/
// allow access to new dashboard if user role is org manager
function isOrgManager(){
$currentUser = wp_get_current_user();
return in_array('org_manager', $currentUser->roles);
}
<?php
if( isOrgManager() ):
// your custom code....
else:
do_action( 'woocommerce_account_navigation' ); ?>
<div >
<?php
/**
* My Account content.
*
* @since 2.6.0
*/
do_action( 'woocommerce_account_content' );
?>
</div>
<?php
endif;