Php wp电子商务-如何判断用户是否购买了产品?
到目前为止,我一直在使用以下代码,在我为特定产品添加变体之前,这些代码一直运行良好:Php wp电子商务-如何判断用户是否购买了产品?,php,wordpress,Php,Wordpress,到目前为止,我一直在使用以下代码,在我为特定产品添加变体之前,这些代码一直运行良好: <?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; ?> 通过查看数据库,我发现产品的每个变体都有一个不同的ID,因
<?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; ?>
通过查看数据库,我发现产品的每个变体都有一个不同的ID,因此它没有注册为购买。那么我如何判断是否购买了任何变体呢?如果有人偶然发现这个问题,我已经找到了答案
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;
}
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;
}