Include dimension letters L/W/H to WooCommerce for

2019-05-20 08:02发布

I'm trying to modify dimensions output that everywhere where get_dimensions() is called instead of default it would include letter of dimension.

So it would be 1 L x 1 W x 1 H instead of 1 x 1 x 1

Can I overwrite get_dimensions() function and include letter alongside each value in array or I can somehow use wc_format_dimensions() to do this?

1条回答
趁早两清
2楼-- · 2019-05-20 08:26

Updated: You can't override get_dimensions() WC_Product method.

But you can use woocommerce_format_dimensions filter hook located in wc_format_dimensions() function, to add custom labels to each dimension product attribute, this way (just as you want):

add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
    if ( empty( $dimension_string ) )
        return __( 'N/A', 'woocommerce' );

    $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) );
    $label_with_dimensions = []; // Initialising

    // Loop Though dimensions array
    foreach( $dimensions as $key => $dimention )
        $label_with_dimensions[$key] = strtoupper( substr($key, 0, 1) ) . ' ' . $dimention;

    return implode( ' x ',  $label_with_dimensions) . ' ' . get_option( 'woocommerce_dimension_unit' );
}

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

This code is tested on WooCommerce versions 3+ and works

查看更多
登录 后发表回答