簡體   English   中英

從產品ID獲取用戶ID的WooCommerce產品購買日期

[英]Get WooCommerce product purchase date from a product Id for a user Id

我遇到了一個問題。 我需要使用用戶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檢查特定的產品購買數據。

我怎樣才能做到這一點?

使用直接自定義SQL查詢,避免循環查看訂單項以找出目標產品,將使數據庫查詢和php過程更加輕松和有效。

該查詢還會檢查所有產品類型, 包括可變產品產品變體

現在我們在這里使用訂單發布日期

所以這里是嵌入在函數中的自定義查詢(返回日期或空值):

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文件中。 經過測試和工作。


USAGE示例 (在當前產品頁面上,對於當前用戶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>';
}

根據您的評論,您需要循環執行訂單並檢查產品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;
}

(未測試)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM