gpt4 book ai didi

php - WooCommerce:将自定义模板添加到客户帐户页面

转载 作者:可可西里 更新时间:2023-10-31 22:52:07 25 4
gpt4 key购买 nike

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

被调用的网址:

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

感谢您的帮助。

最佳答案

这是一个适用于 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';
}

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

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.

引用资料:

关于php - WooCommerce:将自定义模板添加到客户帐户页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38051331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com