Support » Plugin: Cart Weight for WooCommerce » Weight in order emails

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter rapidsuccess

    (@rapidsuccess)

    figured it out of anyone is interested

    /**
     * Add a custom field (in an order) to the emails
     */
    add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
    
    function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
        $fields['order_weight'] = array(
            'label' => __( 'Order Weight' ),
            'value' => get_post_meta( $order->id, 'order_weight', true ),
        );
        return $fields;
    }
    Plugin Support tograczyk

    (@tograczyk)

    Hi there, thanks for your question and thanks for posting a solution.

    If you have any additional questions, just let us know.

    Best regards,
    Tomek

    iamjwal

    (@iamjwal)

    Hey @rapidsuccess – Does this just go in functions.php?

    Thanks for your work!

    iamjwal

    (@iamjwal)

    Sorry for the follow-up. I think I’ve figured out that this goes in the Woocommerce email template email-order-details.php…I just don’t know where.

    Any guidance is appreciated!

    Plugin Support tograczyk

    (@tograczyk)

    Hi @iamjwal, I’m not sure about the solution posted above, but the following code should allow you to display weight in new order email notification.

    add_action('woocommerce_email_after_order_table','show_total_weight', 10, 4);
    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 "<table class='td' cellspacing='0' cellpadding='6' $style1><tr><th $style2>" . __( 'Total weight: ', 'woocommerce' ) . "</th><td $style3>" . $total_weight . " kg</td></tr></table>";
    }

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    The issue is not related to the function of our plugin, and we have not fully tested this code. Here you will find the source of the above solution:

    https://stackoverflow.com/questions/45701473/add-the-order-total-weight-to-woocommerce-new-order-email-notification

    Best regards,
    Tomek

    iamjwal

    (@iamjwal)

    Hey @tograczyk … That worked. Thanks for taking the time to point me in the right direction.

    Plugin Support tograczyk

    (@tograczyk)

    Hi @iamjwal, great to hear that. Always happy to help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.