phr*_*ohn 1 wordpress templates product dimensions woocommerce
有谁知道如何从“单个产品”页面上的其他选项卡中隐藏产品尺寸,但仍然显示“重量”值?
我搜索并看到此过滤器,但它同时隐藏了重量和尺寸。
add_filter( 'woocommerce_product_get_dimensions', '__return_false' );
Run Code Online (Sandbox Code Playgroud)
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() ) : ?>
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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:
Run Code Online (Sandbox Code Playgroud)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; ?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1016 次 |
| 最近记录: |