Last active
December 20, 2015 03:49
-
-
Save RadGH/6066550 to your computer and use it in GitHub Desktop.
These two functions allow you two retrieve the products purchased by a user. One returns an array of all objects with crucial purchase log/cart info, the other returns true or false whether the specific product has been purchased.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wpsc_get_owned_products( $user_id = false ) { | |
global $user_purchases, $wpdb; | |
// Get current user by default | |
if ( !$user_id ) { | |
$current = wp_get_current_user(); | |
if ($current) $user_id = $current->ID; | |
else return false; | |
} | |
$user_id = intval($user_id); | |
// Store user purchases in a global variable to prevent repeat SQL queries | |
// Note that this is not desired if you need to check purchases before and after a checkout on a single page request! | |
if ( !isset($user_purchases) ) $user_purchases = array(); | |
if ( !isset($user_purchases[$user_id]) ) { | |
// SQL request for all of the user's purchases which are "owned" (Processed 3 or 5 -- Accepted Payment or Closed Order) | |
/* | |
$sql = " | |
SELECT * | |
FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` | |
WHERE | |
`user_ID` IN ('" . $user_id . "') | |
AND | |
`processed` IN (3, 5) | |
ORDER BY `date` DESC | |
LIMIT 200"; | |
*/ | |
$sql = " | |
SELECT | |
`logs`.id as 'log_id', | |
`logs`.user_ID as 'user_id', | |
`cart`.id as 'cart_id', | |
`cart`.prodid as 'product_id', | |
`logs`.sessionid as 'sessionid', | |
`logs`.processed as 'processed', | |
`logs`.`date` as 'date' | |
FROM `". WPSC_TABLE_PURCHASE_LOGS ."` AS `logs` | |
INNER JOIN `". WPSC_TABLE_CART_CONTENTS ."` AS `cart` | |
ON `logs`.id = `cart`.purchaseid | |
WHERE | |
`user_ID` IN (" . $user_id . ") | |
AND | |
`processed` IN (3, 5) | |
GROUP BY `cart`.prodid | |
ORDER BY `date` DESC | |
LIMIT 200;"; | |
$user_purchases[$user_id] = $wpdb->get_results( $sql, ARRAY_A ); | |
} | |
/* Return Value: | |
Array ( | |
[0] => Array( | |
[log_id] => 2 | |
[user_id] => 4 | |
[cart_id] => 2 | |
[product_id] => 452 | |
[sessionid] => 6131373064765 | |
[processed] => 3 | |
[date] => 1373064765 | |
), | |
... | |
) | |
*/ | |
return $user_purchases[$user_id]; | |
} | |
function wpsc_user_owns_product( $product_id, $user_id = false ) { | |
global $wpdb; | |
// Get current user by default | |
if ( !$user_id ) { | |
$current = wp_get_current_user(); | |
if ($current) $user_id = $current->ID; | |
else return false; | |
} | |
// Only look for post types: WPSC product | |
if ( get_post_type( $product_id, 'wpsc-product' ) ) { | |
$all_owned_products = wpsc_get_owned_products( $user_id ); | |
foreach( $all_owned_products as $purchase_log_item ) { | |
if ($purchase_log_item['product_id'] == $product_id) | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment