根据用户角色显示自定义主题"my-account.php" woocommerce页面



我们一直在为我的客户即将推出的新数字产品构建一个新的数字仪表板,但是我们希望只有在用户手动提供用户角色的情况下才能访问这个新皮肤和结构化的WooCommerce仪表板(我的帐户模板页面),例如:beta_user, org_manager

任何没有应用此自定义角色的用户将被服务于默认的WooCommerce插件my-account页面。

这是我们尝试过的…

将此代码段添加到子主题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);
}

然后将此应用到/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' );
}?>

对于自定义WP和WooCommerce管理人员来说,这是一个相当普遍的挑战,任何见解都将非常感谢。

首先删除旧文件/child-theme/woocommerce/my-account.php

然后复制/plugins/woocommerce/templates/my-account/my-account.php

to/child-theme/woocommerce/my-account.php

然后添加一个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 WooCommerceTemplates
* @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;

最新更新