有人知道如何在单个产品页面的附加选项卡中隐藏产品尺寸,但仍然显示重量值吗?
我搜索并看到这个过滤器,但它会隐藏重量和尺寸。
add_filter( 'woocommerce_product_get_dimensions', '__return_false' );
有人知道如何在单个产品页面的附加选项卡中隐藏产品尺寸,但仍然显示重量值吗?
我搜索并看到这个过滤器,但它会隐藏重量和尺寸。
add_filter( 'woocommerce_product_get_dimensions', '__return_false' );
只隐藏尺寸而不隐藏重量,有两种方法可以实现。
1) 使用钩子 (这里是复合过滤器钩子):
查看显示单个产品尺寸的模板,你会看到这一行:
<?php if ( $display_dimensions && $product->has_dimensions() ) : ?>
如果您查看WC_Product
has_dimensions()
方法,您会看到这行代码(其中$this
是WC_Product
对象实例):
return ( $this->get_length() || $this->get_height() || $this->get_width() ) && ! $this->get_virtual();
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
。
您需要从模板代码中删除此块(从第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; ?>
single-product/product-attributes.php
已过时...您可以在Woocommerce>状态中检查,在“模板覆盖”部分的页面底部进行检查。 - LoicTheAztecdisplay:none
。