WooCommerce: Assigning an endpoint to a custom template in my account pages

后端 未结 4 1502
情深已故
情深已故 2020-11-27 21:11

This function adds a tab named \"Special Page\" into \"My Account\" tab list:

add_filter( \'woocommerce_account_menu_items\' , \'jc_menu_panel_nav\' );

func         


        
4条回答
  •  青春惊慌失措
    2020-11-27 21:53

    First my-account/special-page/ should be myaccount/special-page/ in woocommerce 2.6+.

    This solution is Incomplete and I am still working On…

    You can use first this hook:

    add_action( 'init', 'add_wc_endpoint' );
    function add_wc_endpoint(){
        add_rewrite_endpoint( 'special-page', EP_ROOT | EP_PAGES );
    }
    

    Then filtering wc_get_templateto call your files when the request match your endpoint:

    add_filter( 'wc_get_template', 'custom_vc_endpoint', 10, 5 );
    function custom_vc_endpoint($located, $template_name, $args, $template_path, $default_path){
    
        if( $template_name == 'myaccount/special-page.php' ){
            global $wp_query;
            if(isset($wp_query->query['special-page'])){
                $located = get_template_directory() . '/woocommerce/myaccount/special-page.php';
            }
        }
    
        return $located;
    }
    

    If you use a child theme, replace get_template_directory() by get_stylesheet_directory()… Paste this code in function.php file of your active child theme or theme.

    To avoid a 404 error "page not found", you will need to refresh rewrite rules adding to your code:

    flush_rewrite_rules();
    

    Update: Finally Dario (the OP) found a working solution. Look at his answer.

    References:

    • Tabbed My Account page (Official Woocommerce 2.6+): Creating new endpoints
    • How to add a new endpoint in woocommerce (old and incomplete)

提交回复
热议问题