wp电子商务 - 如何判断用户是否购买了产品?

时间:2012-06-03 21:27:38

标签: 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; ?>

稍后在代码中,我会使用buy_product变量来查看产品是否已被购买,以便我可以打印特殊消息。

我的问题:由于为很多产品添加了变体,此代码不再有效,它只适用于没有变化的产品......我做错了什么? (顺便说一句,如果你想使用wp电子商务,不要 - 它是垃圾)

以下是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;
}

通过查看数据库,我发现产品的每个变体都有不同的ID,这就是为什么它没有注册为被购买的原因。那我怎么知道是否已经购买了任何变种?

1 个答案:

答案 0 :(得分:3)

如果有人偶然发现了这个问题,我已经找到了答案。

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;
}
相关问题