在WooCommerce我的帐户“订单视图”页面,我应该添加这样的视觉跟踪:
在实际页面上,要跟踪每个订单,以上订单详情:
这是我的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
}
您的代码中存在一些错误:
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_id = $order->get_order_number();
// Get the formatted order date created
$order_id = wc_format_datetime( $order->get_date_created() );
// Get the order status name
$order_id = wc_get_order_status_name( $order->get_status() );
// Display the order status
echo '<p>' . __("Order Status:") . ' ' . $order_status . '</p>';
}
代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。
如果要更改第二个屏幕截图中的黄色下划线文本,则必须在myaccount/view-order.php
模板文件中进行更改:
myaccount/view-order.php
模板文件。<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>