1

到目前为止,我一直在使用以下代码,它一直运行良好,直到我为特定产品添加变体:

<?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; ?>

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

我的问题:由于为许多产品添加了变体,此代码不再适用,它仅适用于没有变体的产品......我做错了什么?(顺便说一句,如果您正在考虑使用 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,所以这就是它没有注册为已购买的原因。那么我如何判断是否购买了任何变体?

4

1 回答 1

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;
}
于 2012-06-03T22:43:40.707 回答