简体   繁体   English

WooCommerce“我的账户”页面如何定义相互关联的菜单和操作?

[英]How to define interrelated menu and action on WooCommerce "My account" page?

I'm trying to create a new menu and action on my account page.我正在尝试在我的帐户页面上创建一个新的菜单和操作。 Menu and action are linked.菜单和操作是链接的。

I want to perform some actions according to the relevant order number on the new page that opens after clicking action.我想在点击操作后打开的新页面上根据相关订单号进行一些操作。 But after clicking the button, the main menu page opens.但是单击按钮后,主菜单页面打开。 How can i solve this problem?我怎么解决这个问题?

The new url would be:新的 url 将是:

There is no problem here.这里没有问题。 However, the main menu content always appears on the opened page.但是,主菜单内容总是出现在打开的页面上。 That is, "Main menu content".即“主菜单内容”。

I'm trying to create a different content for each order status.我正在尝试为每个订单状态创建不同的内容。 Then I will define the main menu content with other actions here.然后我将在这里定义主菜单内容和其他操作。

However, I could not solve this order-based problem.但是,我无法解决这个基于订单的问题。 I examined the WooCommerce codes because it is similar to the View button.我检查了 WooCommerce 代码,因为它类似于“查看”按钮。 However, I couldn't find a solution there either.但是,我在那里也找不到解决方案。

Below is my work:下面是我的工作:

<?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();
}

"I examined the WooCommerce codes because it is similar to the View button" “我检查了 WooCommerce 代码,因为它类似于查看按钮”

You assume that after clicking the view button on the /my-account/orders page, that the url changes to /my-account/orders/1234 but that is not the case, the url currently opening is /my-account/view-order/1234/ , so it display another template file.您假设在/my-account/orders页面上单击查看按钮后,url 变为/my-account/orders/1234但事实并非如此,当前打开的 url 是/my-account/view-order/1234/ ,所以它显示另一个模板文件。

So take this into account in your current code or change the content_custom_menu() callback function to something like:因此,请在您当前的代码中考虑到这一点,或将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM