![](/img/trans.png)
[英]How to add a linked item to another page in WooCommerce My Account menu
[英]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 更改為類似以下內容:
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>';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.