Remove product dimensions from single product pages in Woocommerce

前端 未结 2 1557
栀梦
栀梦 2021-01-14 04:24

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

相关标签:
2条回答
  • 2021-01-14 04:53

    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; ?>
    
    0 讨论(0)
  • 2021-01-14 05:02

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

    0 讨论(0)
提交回复
热议问题