有人知道如何在“单一产品”页面上的附加选项卡中隐藏产品尺寸,但仍显示重量值吗?
我搜索并看到此过滤器,但它隐藏了重量和尺寸。
add_filter( 'woocommerce_product_get_dimensions', '__return_false' );
最佳答案
要仅隐藏尺寸(而不是重量),有2种方法使其发挥作用。
1) 使用钩子(Hook) (这里是复合过滤器钩子(Hook)):
查看显示单个产品尺寸的模板,您可以看到这一行:
<?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();
所以当长度、高度和with为空(或为假)时,该方法返回假...
以下使用复合 Hook 的代码将仅在单个产品页面的“其他信息”选项卡中隐藏维度:
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 模板:
首先阅读:Overriding Woocommerce template via the theme .
它解释了如何在编辑之前将模板复制到您的主题。
这里相关的模板是single-product/product-attributes.php
.
您必须从模板代码中删除此 block (从第 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; ?>
关于wordpress - 从 Woocommerce 中的单个产品页面中删除产品尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52199913/