phraisohn
phraisohn

Reputation: 57

Remove product dimensions from single product pages in Woocommerce

Anyone knows how to hide the product dimensions from the additional tabs on Single Product page but still show the Weight value?

I search and see this filter but it hides both weight and dimensions.

add_filter( 'woocommerce_product_get_dimensions', '__return_false' );

Upvotes: 3

Views: 3873

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254212

To hide only dimensions (but not weight), there is 2 ways to make it work.

1) using hooks (here composite filter hooks):

Looking at the template that displays dimension in single products, you can see this line:

<?php if ( $display_dimensions && $product->has_dimensions() ) : ?>

Then if you look at WC_Product has_dimensions() method, you will see this line (where $this is the WC_Product Object instance):

return ( $this->get_length() || $this->get_height() || $this->get_width() ) && ! $this->get_virtual();

So when the length, the height and the with are empty (or false), the method returns false…

The following code that use composite hooks, will hide dimensions from "Additional information" tab in single product pages only:

add_filter( 'woocommerce_product_get_width', 'hide_single_product_dimentions', 25, 2 );
add_filter( 'woocommerce_product_get_height', 'hide_single_product_dimentions', 25, 2 );
add_filter( 'woocommerce_product_get_length', 'hide_single_product_dimentions', 25, 2 );
function hide_single_product_dimentions( $value, $product ){
    // Only on single product pages
    if( is_product() )
        $value = '';

    return $value;
} 

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

To hide weight (just for info) use this composite hook code:

add_filter( 'woocommerce_product_get_weight', 'hide_single_product_weight', 25, 2 );
  function hide_single_product_weight( $value, $product ){
    // Only on single product pages
    if( is_product() )
        $value = '';

    return $value;
}

2) Overriding Woocommerce templates via your active theme:

First read: Overriding Woocommerce template via the theme.

It explain how to copy the template to your theme before editing it.

Here the related template is single-product/product-attributes.php.

You will have to remove this block from the template code (from line 33 to line 38):

<?php if ( $display_dimensions && $product->has_dimensions() ) : ?>
    <tr>
        <th><?php _e( 'Dimensions', 'woocommerce' ) ?></th>
        <td class="product_dimensions"><?php echo esc_html( wc_format_dimensions( $product->get_dimensions( false ) ) ); ?></td>
    </tr>
<?php endif; ?>

Upvotes: 8

mrk1357
mrk1357

Reputation: 33

You can also use the css property display:none if everything else fails.

Upvotes: 1

Related Questions