【问题标题】:Show users to other users who bought the product in Woocommerce向在 Woocommerce 中购买产品的其他用户展示用户
【发布时间】:2020-11-30 11:40:20
【问题描述】:
我正在四处搜索,但是否可以在 Woocommerce 中显示已在前端购买产品的用户列表?
所以我想在产品页面上显示一个也购买了该产品的用户列表。
【问题讨论】:
标签:
wordpress
woocommerce
【解决方案1】:
好的,我已经通过this 问题的一点帮助修复了它。
所以我把它放在一个函数中:
function retrieve_orders_ids_from_a_product_id( $product_id ) {
global $wpdb;
// Define HERE the orders status to include in <== <== <== <== <== <== <==
$orders_statuses = "'wc-completed'";
# Get All defined statuses Orders IDs for a defined product ID (or variation ID)
return $wpdb->get_col( "
SELECT DISTINCT woi.order_id
FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p
WHERE woi.order_item_id = woim.order_item_id
AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses )
AND woim.meta_key IN ( '_product_id', '_variation_id' )
AND woim.meta_value LIKE '$product_id'
ORDER BY woi.order_item_id DESC"
);
}
然后我用它来显示他们的“显示名称”和个人资料的链接:
$product_id = $product->get_id(); // Put here the product ID.
$orders_ids = retrieve_orders_ids_from_a_product_id( $product_id );
$orderID_array = array_unique($orders_ids);
echo '<ul>';
foreach ( $orderID_array as $orderID ) {
$order = wc_get_order( $orderID );
$profile_name = get_the_author_meta( 'display_name', $order->get_user_id() );
$profile_link = get_author_posts_url($order->get_user_id());
echo '<li><a href="' . $profile_link .'">' . $profile_name . '</a></li>';
}
echo '</ul>';