Answer the question
In order to leave comments, you need to log in
How to display order weight?
Hello!
tell me how to display the weight of the order in orders (in the woocommerce control), as well as in the invoice. In the cart and in the e-mail, I displayed it through the addition in function:
add_action('woocommerce_before_checkout_form', 'bbloomer_print_cart_weight');
add_action('woocommerce_before_cart', 'bbloomer_print_cart_weight');
function bbloomer_print_cart_weight($posted) {
global $woocommerce;
$notice = 'Order weight: ' . $woocommerce->cart->cart_contents_weight . get_option('woocommerce_weight_unit');
if (is_cart()) {
wc_print_notice($notice, 'notice');
} else {
wc_add_notice($notice, 'notice');
}
}
add_action('
function show_total_weight( $order, $sent_to_admin, $plain_text, $email ){
if ( 'new_order' != $email->id ) return;
$total_weight = 0;
foreach( $order->get_items() as $item_id => $product_item ){
$quantity = $product_item->get_quantity(); // get quantity
$product = $product_item->get_product(); // get the WC_Product object
$product_weight = $product->get_weight(); // get the product weight
// Add the line item weight to the total weight calculation
$total_weight += floatval( $product_weight * $quantity );
}
// Styles
$style1 = 'style="width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif; color: #737373; border: 1px solid #e4e4e4; border -top:0;
$style2 = ' style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';
$style3 = ' style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';
// output
echo "
" . __( 'Order weight: ', 'woocommerce' ) . " | " . $total_weight . " kg |
---|
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question