从用户 ID 的产品 ID 获取 WooCommerce 产品购买日期

问题描述 投票:0回答:2

我遇到了一个问题。我需要使用用户 ID 和产品 ID 获取特定的产品购买日期。使用 "检查客户是否已在 WooCommerce 中购买了商品" 答案代码,它会检查以下所有产品:

function has_bought() {
    $customer_orders = get_posts( 
        array(
            'numberposts' => -1,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => 'shop_order', // WC orders post type
            'post_status' => 'wc-completed' // Only orders with status "completed"
        ) 
    );
   return count($customer_orders) > 0 ? true : false; 
}
if( has_bought() )
    echo '<p>You have already maid a purchase</p>';
else
    echo '<p>Welcome, for your first purchase you will get a discount of 10%</p>';

我需要的是使用用户id和产品id来检查特定的产品购买数据。

我怎样才能实现这个目标?

php sql wordpress date woocommerce
2个回答
1
投票

更新 适用于高性能订单存储 (HPOS) 和兼容模式

使用direct自定义更轻且有效的SQL查询,将避免循环遍历订单项来查找目标产品,从而节省服务器资源。

查询会检查所有产品类型,包括可变产品产品变体

对于日期,我们使用订单创建日期。

1)。 对于高性能订单存储 (HPOS) 或启用兼容模式, 将以下内容与更轻量级的优化 SQL 查询结合使用:

function get_product_purchased_last_date( $product_id, $user_id ) {
    global $wpdb;

    return $wpdb->get_var( $wpdb->prepare( "
        SELECT opl.post_date FROM {$wpdb->prefix}wc_order_product_lookup opl
        LEFT JOIN {$wpdb->prefix}wc_orders o ON opl.order_id = o.id
        WHERE o.type = 'shop_order' AND o.status IN ('wc-processing','wc-completed')
        AND o.customer_id = '%d' AND ( opl.product_id' = %d OR opl.variation_id = %d )
        ORDER BY o.id DESC LIMIT 1
    ", $user_id, $product_id, $product_id ) );
}

2)。如果您使用的是 WordPress 帖子存储 (旧版) 且未启用兼容模式,请使用以下内容 (原始代码):

function get_product_purchased_last_date( $product_id, $user_id ) {
    global $wpdb;

    return $wpdb->get_var( $wpdb->prepare( "
        SELECT p.post_date FROM {$wpdb->prefix}posts p
        INNER JOIN {$wpdb->prefix}postmeta pm ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items oi ON oi.order_id = p.ID
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim ON oi.order_item_id = oim.order_item_id
        WHERE p.post_type = 'shop_order' AND p.post_status = 'wc-completed'
        AND pm.meta_key = '_customer_user' AND pm.meta_value = '%d'
        AND oim.meta_key IN ('_product_id','_variation_id') AND oim.meta_value = '%d'
        ORDER BY p.ID DESC LIMIT 1
    ", $user_id, $product_id ) );
}

代码位于活动子主题(或活动主题)的 function.php 文件中。已测试并有效。


使用示例 (在当前产品页面,针对当前用户Id):

if( is_user_logged_in() ) {
    if( $date = get_product_purchased_last_date( get_the_id(), get_current_user_id() ) ) {
        $text = sprintf( __("You have already purchased this product the %s"), date( "jS \of F Y", strtotime($date) ) );
    } else {
        $text =  __("Welcome, for your first purchase you will get a discount of 10%");
    }
    // Display
    echo '<p>' . $text . '</p>';
}

1
投票

根据您的评论,您需要循环遍历订单并检查产品ID是否存在。如果是,则返回日期,否则返回 false。

function check_date_purchased($user_id, $product_id){ 
    $args = array(
        "posts_per_page" => -1,
        "customer_id" => $user_id,
        "orderby" => 'date',
        "order" => 'DESC',
        "status" => "completed", // maybe limit this to completed and processing,
    );
    $query = new WC_Order_Query( $args );
    $orders = $query->get_orders();
    foreach( $orders as $order_id ) {
       $order = wc_get_order( $order_id );
       $items = $order->get_items();
       foreach ($items as $order_item){
          if ($product_id == $order_item->get_product_id()) return $order->order_date;
       }
    }
   return false;
}

(未测试)

© www.soinside.com 2019 - 2024. All rights reserved.