在 WooCommerce 中编辑我的帐户订单视图页面
Editing My account Order view pages in WooCommerce
在 WooCommerce 我的帐户 "Order view" 页面中,我应该添加这样的视觉跟踪:
在实际页面上,跟踪每个订单,以上订单详情:
第一个问题是我不知道如何将 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_id
变量已作为此挂钩的函数参数包含在内,但您的代码中缺少该变量。
- 您不能将
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
模板文件中进行更改:
先读official documentation to understand "how to Override templates via a theme"。
完成并按照文档中的说明将 WooCommerce 模板复制到活动主题后,打开编辑 myaccount/view-order.php
模板文件。
要进行的更改位于第 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>
在 WooCommerce 我的帐户 "Order view" 页面中,我应该添加这样的视觉跟踪:
在实际页面上,跟踪每个订单,以上订单详情:
第一个问题是我不知道如何将 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_id
变量已作为此挂钩的函数参数包含在内,但您的代码中缺少该变量。- 您不能将
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
模板文件中进行更改:
先读official documentation to understand "how to Override templates via a theme"。
完成并按照文档中的说明将 WooCommerce 模板复制到活动主题后,打开编辑
myaccount/view-order.php
模板文件。要进行的更改位于第 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>