【问题标题】: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>';

【讨论】:

    【解决方案2】:

    不确定您的用户是否会喜欢这一点,但我相信 WooCommerce 提供了一个获取订单的钩子。

    $orders->get_items();

    您可以循环访问并获取 user() 信息。显然,您需要确保这些信息不是“个人”信息,因此没有电子邮件等,只有名字。

    检查一下: https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 2014-11-15
      • 1970-01-01
      • 2019-12-29
      • 2020-10-26
      • 2020-01-12
      • 1970-01-01
      • 2016-12-10
      • 2019-08-12
      相关资源
      最近更新 更多