【问题标题】:Display a Tab on the woocommerce my account page conditionally有条件地在 woocommerce 我的帐户页面上显示选项卡
【发布时间】:2016-11-03 10:17:30
【问题描述】:

您好,我能够在 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' ) ) {}

任何想法如何实现这一目标?

尼古拉斯

【问题讨论】:

    标签: php wordpress if-statement woocommerce conditional


    【解决方案1】:

    您可以尝试以下代码,但要工作,您需要在定义 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' );
        }
    } 
    

    【讨论】:

      【解决方案2】:

      您可以在 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>';
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-19
        • 2018-05-23
        • 1970-01-01
        • 1970-01-01
        • 2019-07-29
        • 2016-10-16
        相关资源
        最近更新 更多