我重新安装了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') );
}
}所有都完美地工作着,。直到我在chekout页面中单击"place order",然后我才会看到“内部服务器错误”
你知道如何修复这个错误吗?
我放置了一个调试日志(也许它将帮助您了解正在发生的事情)
[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发布于 2019-12-09 13:51:03
将第二个函数的代码替换为下面的代码来解决这个问题,
// 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这样的变量。
https://stackoverflow.com/questions/59240911
复制相似问题