如何在 WooCommerce "My account" 页面上定义相互关联的菜单和操作?
How to define interrelated menu and action on WooCommerce "My account" page?
我正在尝试在我的帐户页面上创建新的菜单和操作。菜单和操作链接。
我想在点击操作后打开的新页面上根据相关订单号进行一些操作。但是单击按钮后,主菜单页面打开。我该如何解决这个问题?
新的 url 将是:
这里没有问题。但是,主菜单内容总是出现在打开的页面上。即“主菜单内容”。
我正在尝试为每个订单状态创建不同的内容。然后我将在这里定义主菜单内容和其他操作。
但是,我无法解决这个基于顺序的问题。我检查了 WooCommerce 代码,因为它类似于“查看”按钮。但是,我在那里也找不到解决方案。
以下是我的作品:
<?php
/**
* Plugin Name: Custom Menu For Customer
* Desciption: Example description
*
*
*/
defined( 'ABSPATH' ) || exit;
if(!defined('New_Custom_Menu_For_Customers') )
class New_Custom_Menu_For_Customers{
public function __construct() {
add_action( 'init', [ $this, 'rewrite_endpoint' ] );
add_filter( 'query_vars', [ $this, 'add_new_query_vars' ] );
add_filter( 'the_title', [ $this, 'endpoint_title' ] );
add_filter( 'woocommerce_account_menu_items', [ $this, 'my_account_page_order_custom_link' ], 10 );
add_action( 'woocommerce_account_custom-menu_endpoint', [ $this, 'content_custom_menu' ] );
add_filter( 'woocommerce_my_account_my_orders_actions', [ $this, 'order_custom_content_display_button' ], 10, 2 );
}
//Register new endpoint
public function rewrite_endpoint(){
flush_rewrite_rules();
add_rewrite_endpoint( 'custom-menu', EP_ROOT | EP_PAGES );
}
//Register the query vars
public function add_new_query_vars( $vars ){
$vars[] = 'custom-menu';
return $vars;
}
//Define new endpoint
public function my_account_page_order_custom_link( $menu_links ) {
$menu_links['custom-menu'] = __( 'Custom Menu', 'a-text-domain' );
return $menu_links;
}
//Menu content
function content_custom_menu(){
//Get all orders...
echo '<p> Main menu content</p>';
}
//New action button define
public function order_custom_content_display_button( $actions, $order ) {
$url = esc_url_raw( wc_get_account_endpoint_url( 'custom-menu' ) . $order->get_id() );
$actions['custom'] = array( 'url' => $url, 'name' => __( 'Display', 'a-text-domain' ) );
return $actions;
}
/*
New page content after clicking the button...
$order = wc_get_order( $order_id );
...
*/
}
$custom_menu_for_customer = new New_Custom_Menu_For_Customers();
}
“我检查了 WooCommerce 代码,因为它类似于查看按钮”
您假设在单击 /my-account/orders
页面上的查看按钮后,url 变为 /my-account/orders/1234
但事实并非如此,当前的 url打开是 /my-account/view-order/1234/
,所以它显示另一个模板文件。
因此,请在您当前的代码中考虑到这一点,或者将 content_custom_menu()
回调函数更改为类似以下内容:
function content_custom_menu() {
global $wp;
if ( isset( $wp->query_vars['custom-menu'] ) && is_numeric( $wp->query_vars['custom-menu'] ) ) {
echo 'I have an ID = ' . $wp->query_vars['custom-menu'];
} else {
echo '<p>Main menu content</p>';
}
}
我正在尝试在我的帐户页面上创建新的菜单和操作。菜单和操作链接。
我想在点击操作后打开的新页面上根据相关订单号进行一些操作。但是单击按钮后,主菜单页面打开。我该如何解决这个问题?
新的 url 将是:
这里没有问题。但是,主菜单内容总是出现在打开的页面上。即“主菜单内容”。
我正在尝试为每个订单状态创建不同的内容。然后我将在这里定义主菜单内容和其他操作。
但是,我无法解决这个基于顺序的问题。我检查了 WooCommerce 代码,因为它类似于“查看”按钮。但是,我在那里也找不到解决方案。
以下是我的作品:
<?php
/**
* Plugin Name: Custom Menu For Customer
* Desciption: Example description
*
*
*/
defined( 'ABSPATH' ) || exit;
if(!defined('New_Custom_Menu_For_Customers') )
class New_Custom_Menu_For_Customers{
public function __construct() {
add_action( 'init', [ $this, 'rewrite_endpoint' ] );
add_filter( 'query_vars', [ $this, 'add_new_query_vars' ] );
add_filter( 'the_title', [ $this, 'endpoint_title' ] );
add_filter( 'woocommerce_account_menu_items', [ $this, 'my_account_page_order_custom_link' ], 10 );
add_action( 'woocommerce_account_custom-menu_endpoint', [ $this, 'content_custom_menu' ] );
add_filter( 'woocommerce_my_account_my_orders_actions', [ $this, 'order_custom_content_display_button' ], 10, 2 );
}
//Register new endpoint
public function rewrite_endpoint(){
flush_rewrite_rules();
add_rewrite_endpoint( 'custom-menu', EP_ROOT | EP_PAGES );
}
//Register the query vars
public function add_new_query_vars( $vars ){
$vars[] = 'custom-menu';
return $vars;
}
//Define new endpoint
public function my_account_page_order_custom_link( $menu_links ) {
$menu_links['custom-menu'] = __( 'Custom Menu', 'a-text-domain' );
return $menu_links;
}
//Menu content
function content_custom_menu(){
//Get all orders...
echo '<p> Main menu content</p>';
}
//New action button define
public function order_custom_content_display_button( $actions, $order ) {
$url = esc_url_raw( wc_get_account_endpoint_url( 'custom-menu' ) . $order->get_id() );
$actions['custom'] = array( 'url' => $url, 'name' => __( 'Display', 'a-text-domain' ) );
return $actions;
}
/*
New page content after clicking the button...
$order = wc_get_order( $order_id );
...
*/
}
$custom_menu_for_customer = new New_Custom_Menu_For_Customers();
}
“我检查了 WooCommerce 代码,因为它类似于查看按钮”
您假设在单击 /my-account/orders
页面上的查看按钮后,url 变为 /my-account/orders/1234
但事实并非如此,当前的 url打开是 /my-account/view-order/1234/
,所以它显示另一个模板文件。
因此,请在您当前的代码中考虑到这一点,或者将 content_custom_menu()
回调函数更改为类似以下内容:
function content_custom_menu() {
global $wp;
if ( isset( $wp->query_vars['custom-menu'] ) && is_numeric( $wp->query_vars['custom-menu'] ) ) {
echo 'I have an ID = ' . $wp->query_vars['custom-menu'];
} else {
echo '<p>Main menu content</p>';
}
}