Php 在WooCommerce中编辑我的帐户订单视图页面

Php 在WooCommerce中编辑我的帐户订单视图页面,php,wordpress,woocommerce,hook-woocommerce,orders,Php,Wordpress,Woocommerce,Hook Woocommerce,Orders,在我的帐户“订单视图”页面中,我应该添加如下可视跟踪: 在实际页面上,要跟踪每个订单,请参见以上订单详细信息: 第一个问题是我不知道如何将html和php代码添加到视图顺序页面。我尝试在functions.php上添加钩子,但没有成功 第二个问题是,我想在查看订单页面中获取每个订单的状态 (例如:加工或交付等) 下面是我尝试实现它的functions.php代码: // ** // * Add custom tracking code to the view order page

在我的帐户“订单视图”页面中,我应该添加如下可视跟踪:

在实际页面上,要跟踪每个订单,请参见以上订单详细信息:

  • 第一个问题是我不知道如何将html和php代码添加到视图顺序页面。我尝试在functions.php上添加钩子,但没有成功

  • 第二个问题是,我想在查看订单页面中获取每个订单的状态 (例如:加工或交付等)

  • 下面是我尝试实现它的functions.php代码:

        // **
    //  * Add custom tracking code to the view order page
    //  */
    add_action( 'woocommerce_view_order', 'my_custom_tracking' );
    function my_custom_tracking(){
        $order = wc_get_order( $order_id );
    
        $order_id  = $order->get_id(); // Get the order ID
        $parent_id = $order->get_parent_id(); // Get the parent order ID (for subscriptions…)
    
        $user_id   = $order->get_user_id(); // Get the costumer ID
        $user      = $order->get_user(); // Get the WP_User object
    
        echo $order_status  = $order->get_status(); // Get the order status 
    }
    

    您的代码中有一些错误:

  • $order\u id
    变量已作为此挂钩的函数参数包含,但在您的代码中缺少
  • 不能使用
    echo
    $order\u status=$order->get\u status()
  • 因此,请尝试:

    add_action( 'woocommerce_view_order', 'my_custom_tracking' );
    function my_custom_tracking( $order_id ){
        // Get an instance of the `WC_Order` Object
        $order = wc_get_order( $order_id );
        
        // Get the order number
        $order_number  = $order->get_order_number();
        
        // Get the formatted order date created
        $date_created  = wc_format_datetime( $order->get_date_created() );
        
        // Get the order status name
        $status_name  = wc_get_order_status_name( $order->get_status() );
        
        // Display the order status 
        echo '<p>' . __("Order Status:") . ' ' . $status_name . '</p>';
    }
    
     <p><?php
         /* translators: 1: order number 2: order date 3: order status */
         printf(
             __( 'Order #%1$s was placed on %2$s and is currently %3$s.', 'woocommerce' ),
             '<mark class="order-number">' . $order->get_order_number() . '</mark>',
             '<mark class="order-date">' . wc_format_datetime( $order->get_date_created() ) . '</mark>',
             '<mark class="order-status">' . wc_get_order_status_name( $order->get_status() ) . '</mark>'
         );
     ?></p>