Reputation: 4044
So far, I've been using the following code which has been working nicely until I put in variations for a particular product:
<?php if ( is_user_logged_in() ) if ( wpsc_has_purchases() ) if ( wpsc_bought_product(wpsc_the_product_id()) ) $bought_product = true; else $bought_product = false; ?>
Later in the code I would have ussed the bought_product variable to see if the product has been bought so I could print a special message.
My problem: Since putting in variations for a lot of products, this code doesn't work anymore, it only works for products without variations... What am I doing wrong? (btw, if you're thinking of using wp e-commerce, don't - it's junk)
Here's the code for wpsc_bought_product:
function wpsc_bought_product($prodid) {
global $wpdb, $user_ID, $wpsc_purchlog_statuses, $gateway_checkout_form_fields, $purchase_log, $col_count;
do_action( 'wpsc_pre_purchase_logs' );
foreach ( (array)$purchase_log as $purchase ) {
// 2 = order received, 3 = accepted payment
if ($purchase['processed'] == 2) {
$cartsql = $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid`= %d", $purchase['id'] );
$cart_log = $wpdb->get_results( $cartsql, ARRAY_A );
if ( $cart_log != null ) {
foreach ( (array)$cart_log as $cart_row ) {
//if product is in the order
if ($prodid == $cart_row['prodid'])
return true;
}
}
}
}
return false;
}
From looking at the database, I see that there's a different ID for each variation of a product, so that's why it doesn't register as being bought. So how I do I tell if any variation has been bought?
Upvotes: 1
Views: 773
Reputation: 4044
In case someone stumbles upon this question, I've figured out the answer.
function wpsc_bought_product($prodid) {
global $wpdb, $user_ID, $wpsc_purchlog_statuses, $gateway_checkout_form_fields, $purchase_log, $col_count;
$prod_children_sql = $wpdb->prepare( "SELECT `id` FROM `wp_posts` WHERE `post_parent`= %d", $prodid );
$prod_children_result = $wpdb->get_results( $prod_children_sql, ARRAY_A );
$i=0;
foreach($prod_children_result as $inner) {
$prod_children[$i] = current($inner);
$i++;
}
do_action( 'wpsc_pre_purchase_logs' );
foreach ( (array)$purchase_log as $purchase ) {
// 2 = order received, 3 = accepted payment - probably 3 should be here
if ($purchase['processed'] == 2) {
$cartsql = $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid`= %d", $purchase['id'] );
$cart_log = $wpdb->get_results( $cartsql, ARRAY_A );
if ( $cart_log != null ) {
foreach ( (array)$cart_log as $cart_row ) {
//if product is in the order
if ($prod_children != null) {
if (in_array($cart_row['prodid'],$prod_children)) {
return true;
}
} else if ($prodid == $cart_row['prodid']) {
return true;
}
}
}
}
}
return false;
}
Upvotes: 3