Remove product dimensions from single product pages in Woocommerce

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 03:18:11

问题


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' );

回答1:


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; ?>



回答2:


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



来源:https://stackoverflow.com/questions/52199913/remove-product-dimensions-from-single-product-pages-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!