Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Core has a function to see if a user has bought a particular product by ID:

    wc_customer_bought_product()

    https://github.com/woothemes/woocommerce/blob/0da69f2bef9f454945ebbaac581d1073655c8569/includes/wc-user-functions.php#L242-L254

    This query can be adapted to return all, however, it will be slow so be careful with usage and ensure its cached.

    $ids = $wpdb->get_col(
    			$wpdb->prepare( "
    				SELECT im.meta_value FROM {$wpdb->posts} AS p
    				INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
    				INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
    				INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
    				WHERE p.post_status IN ( 'wc-completed', 'wc-processing' )
    				AND pm.meta_key = '_customer_user'
    				AND im.meta_key IN ( '_product_id', '_variation_id' )
    				AND pm.meta_value = %d
    				", $user_id
    			)
    		);
    Thread Starter ikramy

    (@ikramy)

    Thank you

    wc_customer_bought_product() works perfectly.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query products a certain user has purchased/ordered’ is closed to new replies.