简体   繁体   中英

woocommerce checkout page = "internal server error"

I made a fresh install of wordpress 5.3 + woocommerce 3.8.1 + woocommerce Storefront theme .

then I put this code to calculate the weight of each product.

    // Display the cart item weight in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
    if ( $cart_item['data']->get_weight() > 0 ){
        $cart_item_data[] = array(
            'name' => __( 'Weight subtotal', 'woocommerce' ),
            'value' =>  ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit')
        );
    }
    return $cart_item_data;
}

// Save and Display the order item weight (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
function display_order_item_data( $item, $cart_item_key, $values, $order ) {
    if ( $values['data']->get_weight() > 0 ){
        $item->update_meta_data( __( 'Weight subtotal', 'woocommerce' ), ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit') );
    }
}

everything works perfectly . until I click on "place order" in chekout-page then I see "Internal Server Error"

Do you have any idea how to fix this error?

I put a debug log (maybe it will help you understand what's going on)

 [09-Dec-2019 00:06:03 UTC] PHP Notice:  Undefined variable: cart_item in C:\xampp\htdocs\beta\wp-content\plugins\code-snippets\php\snippet-ops.php(361) : eval()'d code on line 17
[09-Dec-2019 00:06:03 UTC] PHP Notice:  Undefined variable: cart_item in C:\xampp\htdocs\beta\wp-content\plugins\code-snippets\php\snippet-ops.php(361) : eval()'d code on line 17
[09-Dec-2019 00:06:03 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_weight() on null in C:\xampp\htdocs\beta\wp-content\plugins\code-snippets\php\snippet-ops.php(361) : eval()'d code:17
Stack trace:
#0 C:\xampp\htdocs\beta\wp-includes\class-wp-hook.php(288): display_order_item_data(Object(WC_Order_Item_Product), '6364d3f0f495b6a...', Array, Object(WC_Order))
#1 C:\xampp\htdocs\beta\wp-includes\class-wp-hook.php(312): WP_Hook->apply_filters('', Array)
#2 C:\xampp\htdocs\beta\wp-includes\plugin.php(478): WP_Hook->do_action(Array)
#3 C:\xampp\htdocs\beta\wp-content\plugins\woocommerce\includes\class-wc-checkout.php(457): do_action('woocommerce_che...', Object(WC_Order_Item_Product), '6364d3f0f495b6a...', Array, Object(WC_Order))
#4 C:\xampp\htdocs\beta\wp-content\plugins\woocommerce\includes\class-wc-checkout.php(386): WC_Checkout->create_order_line_items(Object(WC_Order), Object(WC_Cart))
#5 C:\xampp\htdocs\beta\wp-content\plugins\woocommerce\includes\class-wc-checkout.php(1110): WC_Checkout->create_order(Array)
#6 C:\xampp\htdocs\b in C:\xampp\htdocs\beta\wp-content\plugins\code-snippets\php\snippet-ops.php(361) : eval()'d code on line 17

screenshot

Replace code for second function with below to fix the issue,

// Save and Display the order item weight (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
function display_order_item_data( $item, $cart_item_key, $values, $order ) {
    if ( $values['data']->get_weight() > 0 ){
        $item->update_meta_data( __( 'Weight subtotal', 'woocommerce' ), ( $item['quantity'] * $values['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit') );
    }
}

There is no variable like $cart_item that you have passed in your function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM