• Resolved Daniel

    (@ddumondgmailcom)


    Hello,

    I would like to add Product Weight and Dimensions to the Invoice using the following method but replacing the ‘your_meta_field_name’ with ‘width’ or ‘weight’ does not work. Is this at all possible?

    function example_custom_order_fields( $fields, $order ) {
        $new_fields = array();
            
        if( get_post_meta( $order->get_id(), 'your_meta_field_name', true ) ) {
            $new_fields['your_meta_field_name'] = array( 
                'label' => 'VAT',
                'value' => get_post_meta( $order->get_id(), 'your_meta_field_name', true )
            );
        }
        
        if( get_post_meta( $order->get_id(), 'your_meta_field_name', true ) ) {
            $new_fields['your_meta_field_name'] = array( 
                'label' => 'Customer Number',
                'value' => get_post_meta( $order->get_id(), 'your_meta_field_name', true )
            );
        }
        
        return array_merge( $fields, $new_fields );
    }
    add_filter( 'wcdn_order_info_fields', 'example_custom_order_fields', 10, 2 );
    

    Thank you! ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Daniel

    (@ddumondgmailcom)

    If it helps I was able to display the weight as meta on the admin/order page with:

    add_action( 'woocommerce_after_order_itemmeta', 'zlp_show_weight_admin_order_item_meta', 10, 3 );
    
    function zlp_show_weight_admin_order_item_meta( $item_id, $item, $product ) {
    // Only "line" items and backend order pages
    if( ! ( is_admin() && $item->is_type('line_item') ) ) return;
    // IF PRODUCT OR IT'S VARIATION HAS WEIGHT
    if($product->get_weight()){ ?>
    
    get_weight())." ".get_option('woocommerce_weight_unit'); ?>
    
    <?php }
    }
    moksha shah

    (@mokshasharmila13)

    Hi @ddumondgmailcom

    Yes, this is possible to add new fields in our invoice and the code which you got for custom fields, replacing the ‘your_meta_field_name’ with ‘width’ is correct. You can send an email directly to?[email protected].?

    Please let us know if you have any other query.

    Regards, Moksha.

    Thread Starter Daniel

    (@ddumondgmailcom)

    Hi Moksha,

    I have posted here to help the community, but I can send you an email if you prefer.

    As mentioned in my original post, replacing ‘your_meta_field_name’ with ‘width’ did not work in this case.

    Thanks!
    Daniel

    Thread Starter Daniel

    (@ddumondgmailcom)

    Hey Moksha,

    Thanks again for your help with the cart weight, but I’m still working on displaying the “dimensions”, or at least the “width” of each product.

    Using your sample code below I’ve tried the fields “dimensions” and “_dimensions” and also “width” and “_width”, and none seem to show up on the invoice after submitting a new order.

    Am I using the wrong fields, or is something else missing?

    function invoice_custom_field( $fields, $order ) {
        $new_fields = array();
    
    	// print_r($order); 
        if( get_post_meta( $order->get_id(), '_cart_weight', true ) ) {
            $new_fields['_cart_weight'] = array( 
                'label' => 'Order Weight',
                'value' => get_post_meta( $order->get_id(), '_cart_weight', true )
            );
        }
    
        if( get_post_meta( $order->get_id(), '_dimensions', true ) ) {
            $new_fields['_dimensions'] = array( 
                'label' => 'Dimensions',
                'value' => get_post_meta( $order->get_id(), '_dimensions', true )
            );
        }
        
        return array_merge( $fields, $new_fields );
    }
    add_filter( 'wcdn_order_info_fields', 'invoice_custom_field', 10, 2 );

    Thanks again for your help!! ??</img>
    Daniel

    Thread Starter Daniel

    (@ddumondgmailcom)

    Thanks to the help of ChatGDP I was able to get the dimensions to print, but I’d like them to appear with each product rather than up by the weight… Is this possible?

    Here’s the code I used:

    function invoice_custom_field( $fields, $order ) {
        $new_fields = array();
    
    	// print_r($order); 
        if( get_post_meta( $order->get_id(), '_cart_weight', true ) ) {
            $new_fields['_cart_weight'] = array( 
                'label' => 'Order Weight',
                'value' => get_post_meta( $order->get_id(), '_cart_weight', true )
            );
        }
    
        $items = $order->get_items();
        foreach ($items as $item) 
            $product = $item->get_product();
            $dimensions = wc_format_dimensions( $product->get_dimensions(false) );
            $fields[] = array(
                'label' => 'Dimensions',
                'value' => $dimensions,
            );
        }
        
        return array_merge( $fields, $new_fields );
    }
    add_filter( 'wcdn_order_info_fields', 'invoice_custom_field', 10, 2 );

    Here’s how it shows:

    Please help! ??

    Thread Starter Daniel

    (@ddumondgmailcom)

    Thanks to the awesome help of Moksha, here’s the code to add product dimensions to the invoice…

    Here's the code for your refernce:
    function add_product_dimensions( $product_name, $item ) {
        $product_id    = $item['product_id'];
        $product = $item->get_product();
        if ($product->has_dimensions()) {
            $dimensions = wc_format_dimensions( $product->get_dimensions(false) );
            echo $product_name;
            echo '<br><strong>Dimensions: </strong>' . $dimensions;
        } else {
            echo $product_name;
        }
    }
    add_filter( 'wcdn_order_item_name', 'add_product_dimensions', 10, 2 );
    • This reply was modified 1 year, 9 months ago by Daniel.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add Product Weight and Dimensions to Invoice’ is closed to new replies.