phpwordpresswoocommercecustom-fieldsproduct-variations

How to manage different weight units at the product varition level in WooCommerce?


Referencing this particular question: How to manage different weight units at the product level in WooCommerce?

I have a question in regards to adding something similar to this coding.

How would I get it to apply to the individual variations on a product? Bonus if it gets the weight to appear according to selection on the invoice as well.

(https://i.sstatic.net/JpESdTF2.png)

-- Product Example -- I am offering two versions of the same product but one is Standard Edition (weighs 13oz) and the other is a Special Edition (weighs 1lb 6oz because of bonus material). I want to be able to display those weights in the different weights (lbs & oz).

I tried the suggestion in the referenced post but am unsure how to get it to appear under the individual variations.


Solution

  • for variation products: variation products weight unit option

    checked and worked also for showing in invoice.

    add_action( 'woocommerce_variation_options_dimensions', 'add_variation_weight_unit_option', 10, 3 );
    function add_variation_weight_unit_option( $loop, $variation_data, $variation ) {
        
        $variation_product = wc_get_product($variation->ID);
        $weight_unit = $variation_product->get_meta('_weight_unit');
        
    
        woocommerce_wp_radio(
            array(
                
                'id'          => '_weight_unit[' . $loop . ']',
                'name'        => '_weight_unit[' . $loop . ']',
                'label'       => __( 'Weight Unit', 'woocommerce' ),
                 'desc_tip'    => true,
                'description' => __( 'Select the weight unit for this specific variation.', 'woocommerce' ),
                'options'     => array(
                    'kg'    => __( 'Kg (Default)', 'woocommerce' ),
                    'litre' => __( 'Litre', 'woocommerce' ),
                    'lbs'   => __( 'Pound (lbs)', 'woocommerce' ),
                ),
                
                'value'       => $weight_unit ? $weight_unit : 'kg',
            )
        );
    }
    
    add_action( 'woocommerce_save_product_variation', 'save_variation_weight_unit_option', 10, 2 );
    function save_variation_weight_unit_option( $variation_id, $i ) {
        
        if ( isset( $_POST['_weight_unit'][$i] ) ) {
            $variation = wc_get_product($variation_id);
            if ($variation) {
                 $variation->update_meta_data( '_weight_unit', sanitize_text_field( $_POST['_weight_unit'][$i] ) );
                 $variation->save(); 
            }
        }
    }
    
    add_action( 'wpo_wcpdf_after_item_meta', 'add_custom_weight_unit_to_invoice', 10, 3 );
    function add_custom_weight_unit_to_invoice( $template_type, $item, $order ) {
        
        if ( empty( $item ) ) {
            return;
        }
    
        
        $product = $item['product'];
    
        
        if ( ! $product || ! $product->has_weight() ) {
            return;
        }
    
        $weight = $product->get_weight();
        $unit_key = $product->get_meta('_weight_unit');
    
        
        if ( empty( $unit_key ) ) {
            $unit_key = get_option( 'woocommerce_weight_unit', 'kg' );
        }
    
        
        $unit_label = get_weight_unit_label($unit_key);
    
    
        
        ?>
        <div class="weight">
            <?php _e( 'Weight:', 'woocommerce' ); ?> <?php echo wc_format_localized_decimal( $weight ) . ' ' . esc_html( $unit_label ); ?>
        </div>
        <?php
    }