WooCommerce - 订单索引中的自定义字段重复
我在我的 WooCommerce 订单中添加了一个简单的自定义字段,客户在订购商品时必须提供取货时间。 该字段作为简单的日期字符串保存在 post_meta 表中。
现在我想在 Wordpress 后端的订单索引以及订单详细信息中显示该字段。详细来说,它显示了正确的值,但在索引中它只是显示最新设置日期的重复。
这是我的代码:
add_filter( 'manage_edit-shop_order_columns', 'register_pickup_time_column', 10, 1 );
function register_pickup_time_column( $columns ) {
$columns['pickup_time'] = 'Pickup time';
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'display_pickup_time_column', 10, 1 );
function display_pickup_time_column( $column ) {
global $post;
if ( 'pickup_time' === $column ) {
$pickup_time = get_post_meta( $post->ID, 'Pickup time', true );
echo $pickup_time;
}
}
我交叉检查了数据库中的值与显示不正确的取货时间的订单之一。每个订单的“取货时间”元字段绝对正确。
这怎么可能?
编辑:我发现它似乎失去了自定义列上帖子/订单 ID 的上下文。检查评论:
function display_pickup_time_column( $column ) {
global $post, $woocommerce, $the_order;
$order_id = $the_order->id;
// Correct ID is shown but only in default WooCommerce-Columns
var_dump($order_id);
if ($column === 'pickup_time') {
// Incorrect order ID is shown (duplicate from latest order)!
var_dump($order_id);
}
}
I have added a simple custom field to my WooCommerce orders where customers must provide a pickup time when they order something.
This field gets saved in post_meta table as a simple date string.
Now I wanted to show that field in the orders index in Wordpress Backend and also in order detail. In detail it is showing the correct value but in index it just shows a duplication of the newest set date.
Here's my code:
add_filter( 'manage_edit-shop_order_columns', 'register_pickup_time_column', 10, 1 );
function register_pickup_time_column( $columns ) {
$columns['pickup_time'] = 'Pickup time';
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'display_pickup_time_column', 10, 1 );
function display_pickup_time_column( $column ) {
global $post;
if ( 'pickup_time' === $column ) {
$pickup_time = get_post_meta( $post->ID, 'Pickup time', true );
echo $pickup_time;
}
}
I cross checked the value in database with one of the order which shows an incorrect pickup time. The "Pickup time" meta field is definetly correct for each order.
How is that possible?
EDIT: I have found that it seems to loose the context of the post/order ID on the custom column. Check comments:
function display_pickup_time_column( $column ) {
global $post, $woocommerce, $the_order;
$order_id = $the_order->id;
// Correct ID is shown but only in default WooCommerce-Columns
var_dump($order_id);
if ($column === 'pickup_time') {
// Incorrect order ID is shown (duplicate from latest order)!
var_dump($order_id);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论