在 WooCommerce 我的帐户页面上,根据特定用户角色显示我的地址部分
On WooCommerce My account page, display My Addresses section based on specific user role
在 WooCommerce“我的帐户”页面上,我试图根据用户角色隐藏几个部分。
目前,所有直接使用 WooCommerce 注册表注册的人都被分配了用户角色 'Customer'。但是,实际上只有 'Employer' 角色的用户才能进行购买...所以实际上我想对 'Customers'.
的用户隐藏“我的地址”部分
如果我可以用一个函数做到这一点,有什么想法吗?
米罗
这可以通过模板轻松实现。
在您的 functions.php
文件中添加此函数,以便您可以重复使用它:
function isEmployer(){
$currentUser = wp_get_current_user();
return in_array('employer', $currentUser->roles);
}
从 woocommerce > templates > myaccount
中获取 my-account.php
模板并复制到主题的 WooCommerce 目录 (YOURTHEME > woocommerce > myaccount
)。
从那里转到第 36 行。这是加载地址的地方。
用 PHP if 语句包裹地址,如下所示:
<?php if( isEmployer() ){
wc_get_template( 'myaccount/my-address.php' )
}?>
您需要覆盖主题中的 my-account.php
模板,然后将对地址模板的调用包装在某些条件逻辑中。特别是 current_user_can()
检查 WordPress 功能。
<?php
if( current_user_can( 'place_order' ) ){
wc_get_template( 'myaccount/my-address.php' );
} ?>
理想情况下,您可以根据雇主角色具有而客户角色没有的能力来执行此操作,但在最坏的情况下,您可以使用 current_user_can('employer')
中的角色名称
更新 2021-02-16
鉴于 my-account.php
的重组不再是修改的理想模板,我相信您可以通过 hooks/filters 完全删除部分,而无需覆盖模板。
5.0 现已发布,我可能会过滤 woocommerce_account_menu_items
以从帐户菜单导航中删除项目。然后出于安全目的,也从端点中删除回调...例如,这会将地址内容添加到地址端点:add_action( 'woocommerce_account_edit-address_endpoint', 'woocommerce_account_edit_address' );
因此,要更新我的示例,如果您想完全删除某些用户的“编辑地址”选项卡,您可以使用以下代码段 1. 从“我的帐户”导航中删除该项目,以及 2. 完全禁用该端点。
/**
* Conditionally remove address menu item from My Account.
*
* @param array $items the My Account menu items
* @return array
*/
function so_31342804_remove_address_from_my_account_menu( $items ) {
// Remove menu item for users without certain capability.
if( ! current_user_can( 'place_order' ) ) {
unset( $items['edit-address'] );
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'so_31342804_remove_address_from_my_account_menu' );
/**
* Conditionally remove address endpoint from My Account area.
*
* @param array $items the My Account menu items
* @return array
*/
function so_31342804_remove_address_endpoint( $endpoints ) {
// Remove endpoint content for users without certain capability.
if( ! current_user_can( 'place_order' ) ) {
unset( $endpoints['edit-address'] );
}
return $endpoints;
}
add_filter( 'woocommerce_get_query_vars', 'so_31342804_remove_address_endpoint' );
在 WooCommerce“我的帐户”页面上,我试图根据用户角色隐藏几个部分。
目前,所有直接使用 WooCommerce 注册表注册的人都被分配了用户角色 'Customer'。但是,实际上只有 'Employer' 角色的用户才能进行购买...所以实际上我想对 'Customers'.
的用户隐藏“我的地址”部分如果我可以用一个函数做到这一点,有什么想法吗? 米罗
这可以通过模板轻松实现。
在您的 functions.php
文件中添加此函数,以便您可以重复使用它:
function isEmployer(){
$currentUser = wp_get_current_user();
return in_array('employer', $currentUser->roles);
}
从 woocommerce > templates > myaccount
中获取 my-account.php
模板并复制到主题的 WooCommerce 目录 (YOURTHEME > woocommerce > myaccount
)。
从那里转到第 36 行。这是加载地址的地方。
用 PHP if 语句包裹地址,如下所示:
<?php if( isEmployer() ){
wc_get_template( 'myaccount/my-address.php' )
}?>
您需要覆盖主题中的 my-account.php
模板,然后将对地址模板的调用包装在某些条件逻辑中。特别是 current_user_can()
检查 WordPress 功能。
<?php
if( current_user_can( 'place_order' ) ){
wc_get_template( 'myaccount/my-address.php' );
} ?>
理想情况下,您可以根据雇主角色具有而客户角色没有的能力来执行此操作,但在最坏的情况下,您可以使用 current_user_can('employer')
更新 2021-02-16
鉴于 my-account.php
的重组不再是修改的理想模板,我相信您可以通过 hooks/filters 完全删除部分,而无需覆盖模板。
5.0 现已发布,我可能会过滤 woocommerce_account_menu_items
以从帐户菜单导航中删除项目。然后出于安全目的,也从端点中删除回调...例如,这会将地址内容添加到地址端点:add_action( 'woocommerce_account_edit-address_endpoint', 'woocommerce_account_edit_address' );
因此,要更新我的示例,如果您想完全删除某些用户的“编辑地址”选项卡,您可以使用以下代码段 1. 从“我的帐户”导航中删除该项目,以及 2. 完全禁用该端点。
/**
* Conditionally remove address menu item from My Account.
*
* @param array $items the My Account menu items
* @return array
*/
function so_31342804_remove_address_from_my_account_menu( $items ) {
// Remove menu item for users without certain capability.
if( ! current_user_can( 'place_order' ) ) {
unset( $items['edit-address'] );
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'so_31342804_remove_address_from_my_account_menu' );
/**
* Conditionally remove address endpoint from My Account area.
*
* @param array $items the My Account menu items
* @return array
*/
function so_31342804_remove_address_endpoint( $endpoints ) {
// Remove endpoint content for users without certain capability.
if( ! current_user_can( 'place_order' ) ) {
unset( $endpoints['edit-address'] );
}
return $endpoints;
}
add_filter( 'woocommerce_get_query_vars', 'so_31342804_remove_address_endpoint' );