【发布时间】:2023-03-15 07:29:01
【问题描述】:
我需要计算单笔订单的数量,并将总结果存储在数据库中,然后通过 Rest Api 检索它并在那里访问此参数。 我试图把它写下来,但在结帐时我得到内部服务器错误。
这就是我想要做的(在我的想象中):
// Store volume in the database
add_action('woocommerce_checkout_update_order_meta', 'woo_add_cart_volume');
function woo_add_cart_volume( $order_id ) {
$order = wc_get_order( $order_id ); // <== Was missing
$total_volume = 0; // Initializing variable
foreach( $order->get_items() as $item ){
$product = $item['data'];
$qty = $item['quantity'];
// Get product dimensions
$length = $product->get_length();
$width = $product->get_width();
$height = $product->get_height();
// Calculations a item level
$total_volume += $length * $width * $height * $qty;
}
update_post_meta( $order_id, '_item_volume', $total_volume );
}
感谢您对我的宝贵帮助,请耐心等待。再次感谢您
【问题讨论】:
标签: php wordpress object woocommerce orders