我使用此解决方案来过滤用户产品列表,现在我正在寻找一种解决方案来过滤管理仪表板中的 WooCommerce 订单列表。 也就是说,登录用户只能看到自己发布的产品订单,看不到其他经理产品的订单。
我在 ChatGPT 的帮助下尝试了这段代码,但没有成功:
// Filter orders by products authored by the logged-in user in admin
function filter_orders_by_author_in_admin($query) {
global $pagenow, $post_type;
// Check if it's the orders page in admin and the user is not an administrator
if (is_admin() && $pagenow == 'edit.php' && $post_type == 'shop_order' && !current_user_can('administrator')) {
// Get current user ID
$current_user_id = get_current_user_id();
// Get products authored by the current user
$args = array(
'post_type' => 'product',
'author' => $current_user_id,
'posts_per_page' => -1, // Get all products authored by the user
);
$products_query = new WP_Query($args);
// Extract product IDs
$product_ids = wp_list_pluck($products_query->posts, 'ID');
// Modify the query to only include orders related to products authored by the current user
$meta_query = array(
array(
'key' => '_product_id',
'value' => $product_ids,
'compare' => 'IN',
),
);
// Combine with existing meta queries, if any
$meta_query_combined = $query->get('meta_query');
$meta_query_combined[] = $meta_query;
$query->set('meta_query', $meta_query_combined);
}
}
add_action('pre_get_posts', 'filter_orders_by_author_in_admin');
你能帮我实现这个目标吗?谢谢你
以下将根据产品作者显示订单,以下将仅显示作者的相关订单,在管理订单列表中
1)。对于遗留的“shop_order”帖子:
// Custom function to get orders IDs from product author
function get_orders_by_product_author( $user_id ) {
global $wpdb;
return $wpdb->get_col("
SELECT DISTINCT o.order_id
FROM {$wpdb->prefix}wc_order_product_lookup o
LEFT JOIN {$wpdb->prefix}posts p
ON o.product_id = p.ID
WHERE p.post_author = '{$user_id}'
" );
}
// For classic shop_order posts (doesn't work with High-Performance Order Storage)
add_action( 'pre_get_posts', 'filter_shop_order_posts_by_product_author' );
function filter_shop_order_posts_by_product_author( $query ) {
global $pagenow, $post_type;
if ( is_admin() && 'edit.php' === $pagenow && 'shop_order' === $post_type ) {
$query->set('post__in', get_orders_by_product_author( get_current_user_id() ) );
}
}
2)。对于高性能订单存储 HPOS:
在这里,我们将产品作者添加为新订单的自定义订单元数据,以在启用 HPOS 时过滤订单。
// Utility function: add product author
function add_product_author_to_order_meta( $order ) {
if ( ! $order->get_meta('product_author') && $order->get_type() === 'shop_order' ) {
foreach( $order->get_items() as $item ) {
$product_author_id = get_post_field('post_author', $item->get_product_id() );
$order->add_meta_data('product_author', $product_author_id );
$order->save();
}
}
}
// Add metadata on new order
add_action( 'woocommerce_new_order', 'woocommerce_new_order_callback', 10, 2 );
function woocommerce_new_order_callback( $order_id, $order ) {
add_product_author_to_order_meta( $order );
}
// Add metadata on status changed if not yet set
add_action( 'woocommerce_order_status_changed', 'woocommerce_order_status_changed_callback', 10, 4 );
function woocommerce_order_status_changed_callback( $order_id, $old_status, $new_status, $order ) {
add_product_author_to_order_meta( $order );
}
// Filter order (HPOS)
add_filter( 'woocommerce_order_query_args', 'filter_wc_order_by_product_author' );
function filter_wc_order_by_product_author( $query_args ) {
if ( is_admin() && $_GET['page'] === 'wc-orders' ) {
$query_args['meta_key'] = 'product_author';
$query_args['meta_value'] = get_current_user_id();
return $query_args;
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。