我重新安装了 wordpress 5.3 + woocommerce 3.8.1 + woocommerce店面主题。
然后,我将这段代码用于计算每种产品的重量。
// 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') );
}
}
一切完美。直到我在检查页面上单击“下订单” ,然后看到“内部服务器错误”
您是否知道如何解决此错误?
我放置了一个调试日志(也许它将帮助您了解正在发生的事情)
[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
答案 0 :(得分:1)
用下面的代码替换第二个功能的代码,以解决此问题,
// 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') );
}
}
您没有在函数中传递像$ cart_item这样的变量。