从Woocommerce中的单个产品页面删除产品尺寸 [英] Remove product dimensions from single product pages in Woocommerce

查看:103
本文介绍了从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' );


推荐答案

仅隐藏尺寸 (但没有权重),有 2种方法可以使它起作用。

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

1)使用钩子 (此处为复合过滤器钩子):

查看在单个产品中显示尺寸的模板,您可以看到此行:

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

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

然后,如果您查看 WC_Product has_dimensions()方法,您将看到此行(其中 $ this WC_Product 对象实例)

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

因此,当长度,高度和with为空(或false)时,该方法返回false …

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

代码包含在您活动的子主题的function.php文件中(或活动主题)。经过测试并有效。


隐藏重量 (仅用于信息)使用此功能复合挂钩代码:

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)通过活动主题覆盖Woocommerce模板

首先阅读:通过主题覆盖Woocommerce模板

它说明了如何复制模板

此处相关模板为 single-product / product-attributes.php

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

您将必须从模板代码中删除此块(从第33行到第38行):

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

这篇关于从Woocommerce中的单个产品页面删除产品尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆