【问题标题】:Editing My account Order view pages in WooCommerce在 WooCommerce 中编辑我的帐户订单视图页面
【发布时间】:2019-04-04 09:17:15
【问题描述】:

在 WooCommerce 我的帐户“订单视图”页面中,我应该添加这样的视觉跟踪:

在实际页面上,跟踪每个订单,上面的订单详情:

  1. 第一个问题是我不知道如何将 html 和 php 代码添加到视图订单页面我尝试在 functions.php 上添加钩子但它不起作用

  2. 第二个问题是我想在查看订单页面中获取每个订单的状态 (例如:处理或交付等)

这是我的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 
}

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce orders


    【解决方案1】:

    您的代码中有一些错误:

    1. $order_id 变量已包含在此挂钩的函数参数中,但在您的代码中缺失
    2. 不能使用 echo$order_status = $order->get_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>';
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    如果您想更改第二张屏幕截图中的黄色下划线文本,您必须在 myaccount/view-order.php 模板文件中进行更改:

    1. 首先阅读official documentation了解"how to Override templates via a theme"

    2. 完成并按照文档中的说明将 WooCommerce 模板复制到您的活动主题后,打开编辑 myaccount/view-order.php 模板文件。

    3. 要进行的更改位于第 26 到 34 行:

       <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>
      

    【讨论】:

      猜你喜欢
      • 2018-10-19
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      相关资源
      最近更新 更多