[英]Display a custom themed "my-account.php" woocommerce page based on user role
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我们一直在为我的客户即将推出的新数字产品构建一个新的数字仪表板,但是我们希望仅在手动提供用户的情况下提供对这个新皮肤和结构化的 WooCommerce 仪表板(我的帐户模板页面)的访问权限一个用户角色,即:beta_user、org_manager
Any users who do not have this custom role applied would be served the default WooCommerce plugin my-account pages.任何未应用此自定义角色的用户都将获得默认的 WooCommerce 插件我的帐户页面。
Here is what we have tried...这是我们尝试过的...
Added this snippet to the Child Theme functions.php:将此片段添加到子主题 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:然后将此应用于 /child-theme/woocommerce/my-account.php 文件:
<?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.任何见解都将不胜感激,因为我知道这对于自定义 WP 和 WooCommerce 经理来说是一个相当普遍的挑战。
First remove the old file /child-theme/woocommerce/my-account.php
首先删除旧文件/child-theme/woocommerce/my-account.php
and then copy /plugins/woocommerce/templates/my-account/my-account.php
然后复制/plugins/woocommerce/templates/my-account/my-account.php
to /child-theme/woocommerce/my-account.php
到/child-theme/woocommerce/my-account.php
then add a switch/ if statement something like below:然后添加一个 switch/if 语句,如下所示:
<?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 class="woocommerce-MyAccount-content">
<?php
/**
* My Account content.
*
* @since 2.6.0
*/
do_action( 'woocommerce_account_content' );
?>
</div>
<?php
endif;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.