我重新安装了 wordpress 5.3 + woocommerce 3.8.1 + woocommerce Storefront 主题。
然后我把这段代码用来计算每个产品的重量。
// 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)
交互式爱情