skip to Main Content

In my WooCommerce website is an e-commerce website, some products have a weight Unit in "Kg", some others in "Liter", and Some are in "Pound". In WooCommerce, I can choose a unique unit for Weight. I want it to write a Unit manually by typing Like 5 Liters or 5 Kg or 5 Pounds.

Here is the Screenshot reference:

enter image description here

Any help is appreciated.

2

Answers


  1. It is possible to add an additional field (radio buttons) in the admin product pages next to the weight field (on the shipping tab settings), to manage different weight units at the product level.

    enter image description here

    Here is the code:

    // Add custom fields to product shipping tab
    add_action( 'woocommerce_product_options_dimensions', 'add_product_weight_unit_option');
    function add_product_weight_unit_option(){
        global $product_object;
    
        $weight_unit = $product_object->get_meta('_weight_unit');
    
        woocommerce_wp_radio(
            array(
                'id'          => '_weight_unit',
                'label'       => __( 'Weight unit', 'woocommerce' ),
                'options'     => array(
                    'Kg'        => __( 'Kg (default)', 'woocommerce' ),
                    'Litre'     => __( 'Litre', 'woocommerce' ),
                    'Pound'     => _x( 'Pound', 'Tax status', 'woocommerce' ),
                ),
                'value'       => $weight_unit ? $weight_unit : 'Kg',
            )
        );
    }
    
    // Save custom fields to product shipping tab
    add_action('woocommerce_admin_process_product_object', 'save_google_meet_link_custom_field');
    function save_google_meet_link_custom_field( $product ) {
        if ( isset($_POST['_weight']) && ! empty($_POST['_weight']) && isset($_POST['_weight_unit']) ) {
            $product->update_meta_data( '_weight_unit', esc_attr($_POST['_weight_unit']) );
        } 
    }
    
    // Formating weight with the weight different weight ubits
    add_filter( 'woocommerce_format_weight', 'custom_format_weight', 20, 2 );
    function custom_format_weight( $weight_string, $weight ){
        $weight_string  = wc_format_localized_decimal( $weight );
    
        if ( ! empty( $weight_string ) ) {
            $weight_label = get_post_meta( get_the_ID(), '_weight_unit', true );
    
            $weight_string = sprintf(
                _nx( '%1$s  %2$s', '%1$s  %2$s'.'s', $weight, 'formatted weight', 'woocommerce' ),
                $weight_string,
                $weight_label ? $weight_label : get_option( 'woocommerce_weight_unit' ),
            );
        } else {
            $weight_string = __( 'N/A', 'woocommerce' );
        }
    
        return $weight_string;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    Login or Signup to reply.
  2. Is it possible that after selecting the weight unit: liter, the product unit is "l" not "kg" in several categories?
    screen

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search