1

I would like to display inches and feets dropwon in product height, width and length in wordpress WooCommerce

You can see the products with their title below:enter image description here

Website link: https://asiatic.endroid1.com/product/modern

i have dropdown option in backend wordpress dashboard woocommerce > setting > products.. but not in coding and now i require this option in front end .for example when customer come on products . he select ft or inches from drop down

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

4

In the below code you will find the complete way to get dynamically the dimension unit from a product custom field (select field) backend setting:

// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_dimensions', 'add_product_dimensions_unit_option');
function add_product_dimensions_unit_option(){
    woocommerce_wp_select( array(
        'id'          => '_dimensions_unit',
        'label'       => __( 'Dimensions unit', 'woocommerce' ),
        'options'       => array(
            'in' => __( 'Inch', 'woocommerce' ),
            'ft' => __( 'Foot', 'woocommerce' ),
        ),
    ) );
}

// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_product_dimensions_unit_option' );
function save_product_dimensions_unit_option( $post_id ){
    if( isset( $_POST['_dimensions_unit'] ) )
        update_post_meta( $post_id, '_dimensions_unit', esc_attr( $_POST['_dimensions_unit'] ) );

}

// Dynamic dimension unit from a product custom field
add_filter( 'woocommerce_format_dimensions', 'custom_dimention_unit', 20, 2 );
function custom_dimention_unit( $dimension_string, $dimensions ){
    // Get the dimension unit from product custom field value
    $dimension_unit = get_post_meta( get_the_id(), '_dimensions_unit', true );

    // HERE set the Default dimention unit  <=========  <=========  <=========
    if( empty($dimension_unit) ) $dimension_unit = 'in';

    $dimension_string = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );

    if ( ! empty( $dimension_string ) ) {
        $dimension_string .= ' ' . $dimension_unit;
    } else {
        $dimension_string = __( 'N/A', 'woocommerce' );
    }

    return $dimension_string;
}

This code goes on function.php file of your active child theme (or active theme). tested and works.

In backend product edit pages settings:

enter image description here

In front end:

enter image description here


The code is based on this two related answers:


Additional code for product variations, in backend:

// Add custom fields to product variation settings
add_action( 'woocommerce_product_after_variable_attributes','add_variation_dimensions_unit_option', 10, 3 );
function add_variation_dimensions_unit_option( $loop, $variation_data, $variation ){

    $dimensions_unit = get_post_meta($variation->ID,"_dimensions_unit", true );
    if( ! $dimensions_unit ) $dimensions_unit = "";

    echo '<p class="form-field dimensions_field">';
    woocommerce_wp_select( array(
        'id'          => '_dimensions_unit' . '_' . $loop,
        'label'       => __( 'Dimensions unit', 'woocommerce' ),
        'options'       => array(
            'in' => __( 'Inches', 'woocommerce' ),
            'ft' => __( 'Feet', 'woocommerce' ),
        ),
        'value'       => $variation_diameter
    ) );
    echo '</p>';
}

// Save product variation custom fields values
add_action( 'woocommerce_save_product_variation','save_variation_dimensions_unit_option', 10 ,2 );
function save_variation_dimensions_unit_option( $variation_id, $loop ){
    $built_lenght = $_POST["_dimensions_unit_$loop"];
    if( isset($built_lenght) )
        update_post_meta( $variation_id, '_dimensions_unit', esc_attr($built_lenght) );
}

This code goes on function.php file of your active child theme (or active theme). tested and works.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks your first code is working well and good.......................but second code is not working........................i want to show drop down inches and feet in single products view in front end......when customer click on drop down ..if customer select inches products show in inches and if customer select feet products show their dimension in feet...........again thanks – Asiatics Group of Companies Jul 17 '18 at 05:22
  • Is there a way to edit this from the "WooCommerce->Settings->General->Products->Measurements" settings? Here's an image explanation: https://imgur.com/D5t321T – chromechris Feb 26 '20 at 03:50
  • 1
    @chromechris Maybe try: https://stackoverflow.com/a/51308366/3730754 – LoicTheAztec Feb 26 '20 at 08:11