要仅隐藏尺寸 (但不隐藏重量),有 2 种方法可以使其发挥作用。
1) 使用钩子 (这里是复合过滤器钩子):
查看显示单品维度的模板,可以看到这一行:
<?php if ( $display_dimensions && $product->has_dimensions() ) : ?>
然后如果您查看WC_Product has_dimensions() method,您将看到这一行(其中$this 是WC_Product 对象实例):
return ( $this->get_length() || $this->get_height() || $this->get_width() ) && ! $this->get_virtual();
所以当length、height和with为空(或false)时,该方法返回false……
以下使用复合挂钩的代码将仅在单个产品页面的“附加信息”选项卡中隐藏尺寸:
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 文件中。经过测试并且可以工作。
隐藏重量 (仅供参考)使用此复合挂钩代码:
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 模板:
初读:Overriding Woocommerce template via the theme。
它解释了如何在编辑之前将模板复制到您的主题中。
这里的相关模板是single-product/product-attributes.php。
您必须从模板代码中删除此块(从第 33 行到第 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; ?>