[英]On WooCommerce My account page, display My Addresses section based on specific user role
On the WooCommerce My Account page I am trying to hide a couple of the sections based on the user role.在 WooCommerce 我的帐户页面上,我试图根据用户角色隐藏几个部分。
At the moment, all people who register directly with the WooCommerce registration form are assigned user role 'Customer'.目前,所有直接使用 WooCommerce 注册表注册的人都被分配了用户角色“客户”。 However, only users with role 'Employer' are actually able to make purchases... so effectively I want to hide the My Addresses section to users who are 'Customers'.但是,实际上只有具有“雇主”角色的用户才能进行购买……因此我想对“客户”用户隐藏“我的地址”部分。
Any ideas if I can do this with a function?如果我可以用函数来做到这一点,有什么想法吗? Miro米罗
This is possible easily with templates.这可以通过模板轻松实现。 Add this function in your functions.php
file so you can reuse it:在您的functions.php
文件中添加这个函数,以便您可以重用它:
function isEmployer(){
$currentUser = wp_get_current_user();
return in_array('employer', $currentUser->roles);
}
Grab the my-account.php
template from woocommerce > templates > myaccount
and copy over to your theme's WooCommerce directory ( YOURTHEME > woocommerce > myaccount
).从woocommerce > templates > myaccount
my-account.php
模板,然后复制到您主题的 WooCommerce 目录( YOURTHEME > woocommerce > myaccount
)。
From there go to line 36. THIS is where the address gets loaded in.从那里转到第 36 行。这是地址被加载的地方。
Wrap the address with a PHP if statement like so:用 PHP if 语句包裹地址,如下所示:
<?php if( isEmployer() ){
wc_get_template( 'myaccount/my-address.php' )
}?>
You would need to override the my-account.php
template in your theme and then wrap the call to the address template in some conditional logic.您需要覆盖主题中的my-account.php
模板,然后在某些条件逻辑中包装对地址模板的调用。 Specificallycurrent_user_can()
which checks for WordPress capabilities.特别是current_user_can()
用于检查 WordPress 功能。
<?php
if( current_user_can( 'place_order' ) ){
wc_get_template( 'myaccount/my-address.php' );
} ?>
Ideally, you would do this based on a capability that the Employer role has that the Customer role does not, but in the worst case you could use the role name à la current_user_can('employer')
理想情况下,您将根据 Employer 角色具有而 Customer 角色没有的功能来执行此操作,但在最坏的情况下,您可以使用角色名称 à la current_user_can('employer')
Update 2021-02-16更新 2021-02-16
Given the restructuring of my-account.php
that is no longer the ideal template to modify and I believe you could remove sections entirely via hooks/filters without overriding a template.鉴于my-account.php
的重组不再是理想的修改模板,我相信您可以通过钩子/过滤器完全删除部分而无需覆盖模板。
5.0 is out right now, and I would probably filter woocommerce_account_menu_items
to remove items from the account menu navigation. 5.0 现已发布,我可能会过滤woocommerce_account_menu_items
以从帐户菜单导航中删除项目。 And then for security purposes remove the callback from the endpoint too... As an example this adds the address content to the address endpoint: add_action( 'woocommerce_account_edit-address_endpoint', 'woocommerce_account_edit_address' );
然后出于安全目的也从端点删除回调......作为一个例子,这将地址内容添加到地址端点: add_action( 'woocommerce_account_edit-address_endpoint', 'woocommerce_account_edit_address' );
So to update my example, if you want to completely remove the Edit Addresses tab for certain users, you could use the following snippet to 1. remove the item from the My Account navigation and 2. completely disable that endpoint.因此,要更新我的示例,如果您想完全删除某些用户的“编辑地址”选项卡,您可以使用以下代码段来 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' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.