[英]Display a Tab on the woocommerce my account page conditionally
您好,我能夠在woocommerce我的帳戶頁面上創建自定義端點:
function my_custom_my_account_menu_items( $items ) {
$items = array(
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
//'downloads' => __( 'Downloads', 'woocommerce' ),
//'edit-address' => __( 'Addresses', 'woocommerce' ),
//'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
'edit-account' => __( 'Edit Account', 'woocommerce' ),
'special-page' => 'Special Page',
'customer-logout' => __( 'Logout', 'woocommerce' ),
);
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
我訪問了以下URL: https : //github.com/woocommerce/woocommerce/wiki/2.6-Tabbed-My-Account-page WooCommerce:將端點分配給我的帳戶頁面中的自定義模板
現在,我想知道如何根據用戶角色有條件地顯示選項卡? 我只想為供應商顯示“特殊頁面”選項卡。
if ( ! current_user_can( 'vendor' ) ) {}
任何想法如何實現這一目標?
尼古拉斯
您可以嘗試以下代碼,但是要工作,需要在定義current_user時調用此部分。
add_action('plugin_loaded', 'vendor_menu_items');
function vendor_menu_items(){
if ( ! current_user_can( 'vendor' ) ) {
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
}
}
您可以在add_filter語句周圍或函數主體內部添加條件用戶角色。 在這種情況下,這並不重要,因為這是一種快速檢查。 這是用於將端點添加到woocommerce我的帳戶頁面的完整代碼:
// either use this line if you're writing a plugin
register_activation_hook(__FILE__, 'flush_rewrite_rules');
// or use this line if you're writing a theme
add_action('switch_theme', 'flush_rewrite_rules');
add_action('init', 'my_add_rewrite_endpoints');
function my_add_rewrite_endpoints() {
add_rewrite_endpoint('vendor', EP_ROOT | EP_PAGES);
}
add_filter('woocommerce_account_menu_items', 'my_my_account_menu_items');
function my_my_account_menu_items($menu_items) {
// remove items this way
unset($menu_items['downloads']);
unset($menu_items['edit-address']);
unset($menu_items['payment-methods']);
// add items this way
if(current_user_can('vendor')) {
$menu_items['vendor'] = __('Your vendor page', 'your-plugin-domain-name');
}
return $menu_items;
}
// note the key you used for $menu_items in the previous function must be in the name of this filter hook.
add_filter('woocommerce_account_vendor_endpoint', 'my_vendor_endpoint');
function my_vendor_endpoint() {
echo '<p>Your page content here</p>';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.