WooCommerce:向客户帐户页面添加自定义模板 [英] WooCommerce: Adding custom template to customer account pages

查看:67
本文介绍了WooCommerce:向客户帐户页面添加自定义模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

被调用的网址:

/my-account/edit-order/55/

这是在 functions.php 文件中,带有端点集和模板覆盖:

//工作add_action('init', 'add_endpoint');函数 add_endpoint(){add_rewrite_endpoint('edit-order', EP_ALL);}//这里需要一些东西来检查端点并将页面作为 woocommerce 运行//无法测试add_filter('wc_get_template', 'custom_endpoint', 10, 5);函数 custom_endpoint($located, $template_name, $args, $template_path, $default_path){if( $template_name == 'myaccount/my-account.php' ){全局 $wp_query;if(isset($wp_query->query['edit-order'])){$located = get_template_directory() .'/woocommerce/myaccount/edit-order.php';}}返回 $location;}

感谢您的帮助.

解决方案

这是 WooCommerce 2.6+ 的工作解决方案,用于扩展和操作 标签式我的帐户"页面端点(请参阅此参考 在此答案的末尾),因此您可以通过以下方式实现:

add_action( 'init', 'custom_new_wc_endpoint' );函数 custom_new_wc_endpoint() {add_rewrite_endpoint('edit-order', EP_ROOT | EP_PAGES);}add_filter( 'query_vars', 'custom_query_vars', 0 );函数 custom_query_vars( $vars ) {$vars[] = '编辑顺序';返回 $vars;}add_action('after_switch_theme', 'custom_flush_rewrite_rules');函数 custom_flush_rewrite_rules() {flush_rewrite_rules();}//自定义模板位置add_action('woocommerce_account_edit-order_endpoint', 'custom_endpoint_content');函数 custom_endpoint_content() {包括woocommerce/myaccount/edit-order.php";}


然后您需要将新的编辑订单端点插入我的帐户菜单:

add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );函数 custom_my_account_menu_items( $items ) {//删除订单菜单项.$orders_item = $items['orders'];//首先我们将它保存在一个变量中取消设置( $items['orders'] );//然后我们取消它//插入您的自定义端点.$items['edit-order'] = __('Edit Order', 'woocommerce');//插回注销项.$items['orders'] = $orders_item;//我们把它放回去返回 $items;}


<块引用>

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

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

参考文献:

I am attempting to add a custom page to the customers 'account' section, which will allow the user to edit their order. At present I have been able to set an end point for the url and pick it up, but I need to get WooCommerce to initiate the page layout and be able to set the template location.

The URL being called:

/my-account/edit-order/55/

This is in the functions.php file, with the end point set and template override:

// 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;
}

Thanks for any help.

解决方案

This is a working solution for WooCommerce 2.6+ to extend and manipulate the tabbed "My Account" page endpoints (See this reference at the end of this answer), so here it is what you can do to achieve this:

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'; 
}


Then you will need, to Insert the new Edit order endpoint into the My Account menu:

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;
}


Important: You will need to flush the rewrite rules (2 ways):

  • Go to the Permalinks options page and re-save the permalinks (thanks to helgatheviking)
  • You can also disable/enable your theme.

References:

这篇关于WooCommerce:向客户帐户页面添加自定义模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆