There are some missing parts in your code:
- The
$order
need to be defined before using get_items()
method on it,
- Also
$total_volume
need to be initialized before.
Use instead:
// 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_volume = (float) get_post_meta( $item->get_product_id(), '_item_volume', true );
$total_volume += $product_volume * $item->get_quantity();
}
update_post_meta( $order_id, '_item_volume', $total_volume );
}
Now it should better work.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…