您好我可以在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' );
我访问了以下网址: https://github.com/woocommerce/woocommerce/wiki/2.6-Tabbed-My-Account-page WooCommerce: Assigning an endpoint to a custom template in my account pages
现在我想知道如何根据用户角色有条件地显示标签? 我想展示"特别页面"标签仅供供应商使用。
if ( ! current_user_can( 'vendor' ) ) {}
任何想法如何实现这一目标?
尼古拉斯
答案 0 :(得分:0)
您可以尝试以下代码,但是为了工作,您需要在定义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' );
}
}
答案 1 :(得分:0)
您可以在add_filter语句周围或在函数体内部添加条件的用户角色。在这种情况下,这并不重要,因为它是一个快速检查。 以下是将端点添加到woocommerce my-account页面的完整代码:
// 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>';
}