PHP 已弃用:不推荐创建动态属性 WPSC_Cart::$log_id

PHP Deprecated: Creation of dynamic property WPSC_Cart::$log_id is deprecated

提问人:user21167050 提问时间:10/8/2023 更新时间:10/8/2023 访问量:69

问:

在修复了一些动态属性弃用警告后,刚刚升级到 PHP 8.2。

以为一切都已得到解决,升级后从错误日志中发现了一个剩余的问题,正在努力修复。

对php来说仍然很新,所以可能是我错过了一些明显的东西。

我苦苦挣扎的原因是因为该文件中的任何 php 代码中都没有提到类,它与所有其他动态属性警告一起出现在函数的正上方,可以通过添加例如:

public $log_id;

与此错误相关的代码如下:

function wpsc_submit_checkout( $collected_data = true ) {
global $wpdb, $wpsc_cart, $user_ID, $nzshpcrt_gateways, $wpsc_shipping_modules, $wpsc_gateways;

if ( $collected_data && isset( $_POST['collected_data'] ) && is_array( $_POST['collected_data'] ) ) {
    _wpsc_checkout_customer_meta_update( $_POST['collected_data'] );
}

// initialize our checkout status variable, we start be assuming
// checkout is falid, until we find a reason otherwise
$is_valid           = true;
$num_items          = 0;
$use_shipping       = 0;
$disregard_shipping = 0;

do_action( 'wpsc_before_submit_checkout', $collected_data );

$error_messages = wpsc_get_customer_meta( 'checkout_misc_error_messages' );

if ( ! is_array( $error_messages ) ) {
    $error_messages = array();
}

$wpsc_checkout = new wpsc_checkout();

$selected_gateways = get_option( 'custom_gateway_options' );
$submitted_gateway = isset( $_POST['custom_gateway'] ) ? $_POST['custom_gateway'] : '';

if ( $collected_data ) {
    $form_validity = $wpsc_checkout->validate_forms();
    extract( $form_validity ); // extracts $is_valid and $error_messages

    if ( wpsc_has_tnc() && ( ! isset( $_POST['agree'] ) || $_POST['agree'] != 'yes' ) ) {
        $error_messages[] = __( 'Please agree to the terms and conditions, otherwise we cannot process your order.', 'wp-e-commerce' );
        $is_valid = false;
    }
} else {
    $is_valid = true;
    $error_messages = array();
}

if ( wpsc_uses_shipping() ) {
    $wpsc_country = new WPSC_Country( wpsc_get_customer_meta( 'shippingcountry' ) );
    $country_id   = $wpsc_country->get_id();
    $country_name = $wpsc_country->get_name();

    foreach ( $wpsc_cart->cart_items as $cartitem ) {

        if ( ! empty( $cartitem->meta[0]['no_shipping'] ) ) {
            continue;
        }

        $category_ids = $cartitem->category_id_list;

        foreach ( (array) $category_ids as $catid ) {
            if ( is_array( $catid ) ) {
                $countries = wpsc_get_meta( $catid[0], 'target_market', 'wpsc_category' );
            } else {
                $countries = wpsc_get_meta( $catid, 'target_market', 'wpsc_category' );
            }

            if ( ! empty( $countries ) && ! in_array( $country_id, (array) $countries ) ) {
                $errormessage = sprintf( __( '%s cannot be shipped to %s. To continue with your transaction, please remove this product from the list below.', 'wp-e-commerce' ), $cartitem->get_title(), $country_name );
                wpsc_update_customer_meta( 'category_shipping_conflict', $errormessage );
                $is_valid = false;
            }
        }

        //count number of items, and number of items using shipping
        $num_items++;

        if ( $cartitem->uses_shipping != 1 ) {
            $disregard_shipping++;
        } else {
            $use_shipping++;
        }
    }
}

// check to see if the current gateway is in the list of available gateways
if ( array_search( $submitted_gateway, $selected_gateways ) !== false || wpsc_is_free_cart() ) {
    wpsc_update_customer_meta( 'selected_gateway', $submitted_gateway );
} else {
    $is_valid = false;
}

if ( $collected_data ) {

    // Test for required shipping information
    if ( wpsc_core_shipping_enabled() && ( $num_items != $disregard_shipping ) ) {
        // for shipping to work we need a method, option and a quote, unless we have free shipping.

        $shipping_discount_value  = get_option( 'shipping_discount_value' );
        $is_free_shipping_enabled = get_option( 'shipping_discount' );
        $subtotal                 = $wpsc_cart->calculate_subtotal();

        $has_free_shipping = $is_free_shipping_enabled && $shipping_discount_value > 0 && $shipping_discount_value <= $subtotal;

        if ( ! $has_free_shipping ) {
            if ( ! $wpsc_cart->shipping_method_selected() || ! $wpsc_cart->shipping_quote_selected() ) {
                $error_messages[] = __( 'Please select one of the available shipping options, then we can process your order.', 'wp-e-commerce' );
                $is_valid = false;
            }
        }

        // if we don't have a valid zip code ( the function also checks if we need it ) we have an error
        if ( ! wpsc_have_valid_shipping_zipcode() ) {
                wpsc_update_customer_meta( 'category_shipping_conflict', __( 'Please enter a Zipcode and click calculate to proceed', 'wp-e-commerce' ) );
                $is_valid = false;
        }
    }
}

wpsc_update_customer_meta( 'checkout_misc_error_messages', $error_messages );

if ( $is_valid == true ) {
    wpsc_delete_customer_meta( 'category_shipping_conflict' );
    // check that the submitted gateway is in the list of selected ones
    $sessionid = (mt_rand( 100, 999 ) . time());
    wpsc_update_customer_meta( 'checkout_session_id', $sessionid );
    $subtotal = $wpsc_cart->calculate_subtotal();
    if ( $wpsc_cart->has_total_shipping_discount() == false )
        $base_shipping = $wpsc_cart->calculate_base_shipping();
    else
        $base_shipping = 0;

    $delivery_country = $wpsc_cart->delivery_country;
    $delivery_region = $wpsc_cart->delivery_region;

    if ( wpsc_uses_shipping() ) {
        $shipping_method = $wpsc_cart->selected_shipping_method;
        $shipping_option = $wpsc_cart->selected_shipping_option;
    } else {
        $shipping_method = '';
        $shipping_option = '';
    }
    if ( isset( $_POST['how_find_us'] ) )
        $find_us = $_POST['how_find_us'];
    else
        $find_us = '';

    //keep track of tax if taxes are exclusive
    $wpec_taxes_controller = new wpec_taxes_controller();
    if ( ! $wpec_taxes_controller->wpec_taxes_isincluded() ) {
        $tax = $wpsc_cart->calculate_total_tax();
        $tax_percentage = $wpsc_cart->tax_percentage;
    } else {
        $tax = 0.00;
        $tax_percentage = 0.00;
    }

    $total = $wpsc_cart->calculate_total_price();

    $args = array(
        'totalprice'       => $total,
        'statusno'         => '0',
        'sessionid'        => $sessionid,
        'user_ID'          => (int) $user_ID,
        'date'             => time(),
        'gateway'          => $submitted_gateway,
        'billing_country'  => $wpsc_cart->selected_country,
        'shipping_country' => $delivery_country,
        'billing_region'   => $wpsc_cart->selected_region,
        'shipping_region'  => $delivery_region,
        'base_shipping'    => $base_shipping,
        'shipping_method'  => $shipping_method,
        'shipping_option'  => $shipping_option,
        'plugin_version'   => WPSC_VERSION,
        'discount_value'   => $wpsc_cart->coupons_amount,
        'discount_data'    => $wpsc_cart->coupons_name,
        'find_us'          => $find_us,
        'wpec_taxes_total' => $tax,
        'wpec_taxes_rate'  => $tax_percentage,
    );

    $purchase_log = new WPSC_Purchase_Log( $args );
    $purchase_log->save();
    $purchase_log_id = $purchase_log->get( 'id' );

    //Check to ensure log row was inserted successfully
    if(is_null($purchase_log_id)) {
        $error_messages[] = __( 'A database error occurred while processing your request.', 'wp-e-commerce' );
        wpsc_update_customer_meta( 'checkout_misc_error_messages', $error_messages );
        return;
    }

    if ( $collected_data ) {
        $wpsc_checkout->save_forms_to_db( $purchase_log_id );
    }

    $wpsc_cart->save_to_db( $purchase_log_id );
    $wpsc_cart->submit_stock_claims( $purchase_log_id );

    if ( ! isset( $our_user_id ) && isset( $user_ID ) ) {
        $our_user_id = $user_ID;
    }

    $wpsc_cart->log_id = $purchase_log_id;
    do_action( 'wpsc_submit_checkout', array( 'purchase_log_id' => $purchase_log_id, 'our_user_id' => $our_user_id ) );
    do_action( 'wpsc_submit_checkout_gateway', $submitted_gateway, $purchase_log );
}

}

这是与上述错误相关的代码行:

    $wpsc_cart->log_id = $purchase_log_id;

任何帮助或建议将不胜感激!

PHP 动态 属性 警告已 弃用

评论

0赞 Nigel Ren 10/9/2023
您需要找出哪个类是哪个类的实例,然后尝试将属性添加到该类中。$wpsc_cartecho get_class($wpsc_cart);
0赞 user21167050 10/9/2023
感谢 Nigel,通过浏览其他一些文件,设法找出了 $wpsc_cart 类是哪个类的实例。新增 public $log_id;添加到另一个文件,这已经解决了问题。感谢您对此的帮助。

答: 暂无答案