【问题标题】:WooCommerce: Adding custom template to customer account pagesWooCommerce:将自定义模板添加到客户帐户页面
【发布时间】:2016-06-27 10:15:54
【问题描述】:

我正在尝试将自定义页面添加到客户的“帐户”部分,这将允许用户编辑他们的订单。目前我已经能够为 url 设置一个端点并提取它,但我需要让 WooCommerce 启动页面布局并能够设置模板位置。

被调用的 URL:

/my-account/edit-order/55/

这在 functions.php 文件中,设置了端点并覆盖了模板:

// Working
add_action( 'init', 'add_endpoint' );
function add_endpoint(){
     add_rewrite_endpoint( 'edit-order', EP_ALL );
}

// need something here to check for end point and run page as woocommerce

// Not been able to test
add_filter( 'wc_get_template', 'custom_endpoint', 10, 5 );
function custom_endpoint($located, $template_name, $args, $template_path, $default_path){

    if( $template_name == 'myaccount/my-account.php' ){
        global $wp_query;
        if(isset($wp_query->query['edit-order'])){
            $located = get_template_directory() . '/woocommerce/myaccount/edit-order.php';
        }
    }

    return $located;
}

感谢您的帮助。

【问题讨论】:

    标签: php wordpress woocommerce endpoint account


    【解决方案1】:

    这是 WooCommerce 2.6+ 扩展和操作选项卡式“我的帐户”页面端点的有效解决方案(请参阅此答案末尾的 this reference) ,所以这是你可以做的:

    add_action( 'init', 'custom_new_wc_endpoint' );
    function custom_new_wc_endpoint() {
        add_rewrite_endpoint( 'edit-order', EP_ROOT | EP_PAGES );
    }
    
    add_filter( 'query_vars', 'custom_query_vars', 0 );
    function custom_query_vars( $vars ) {
        $vars[] = 'edit-order';
        return $vars;
    }
    
    add_action( 'after_switch_theme', 'custom_flush_rewrite_rules' );    
    function custom_flush_rewrite_rules() {
        flush_rewrite_rules();
    }
    
    // The custom template location
    add_action( 'woocommerce_account_edit-order_endpoint', 'custom_endpoint_content' );
    function custom_endpoint_content() {
        include 'woocommerce/myaccount/edit-order.php'; 
    }
    

    然后您需要将新的 Edit order 端点插入到 我的帐户菜单

    add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
    function custom_my_account_menu_items( $items ) {
        // Remove the orders menu item.
        $orders_item = $items['orders']; // first we keep it in a variable
        unset( $items['orders'] ); // we unset it then
    
        // Insert your custom endpoint.
        $items['edit-order'] = __( 'Edit Order', 'woocommerce' );
    
        // Insert back the logout item.
        $items['orders'] = $orders_item; // we set it back
    
        return $items;
    }
    

    重要提示:您需要刷新重写规则 (两种方式)

    • 转到永久链接选项页面并重新保存永久链接(感谢helgatheviking
    • 您还可以禁用/启用您的主题。

    参考资料:

    【讨论】:

    • 要刷新重写规则,您还可以转到永久链接选项页面并重新保存永久链接。
    • @helgatheviking 永远是 woocommerce 的好仙女 :) ...我现在将添加此内容。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-05-30
    • 2022-01-03
    • 2015-03-17
    • 1970-01-01
    • 2018-01-03
    • 2018-05-15
    • 1970-01-01
    相关资源
    最近更新 更多