从Woocommerce的单个产品页面中删除产品尺寸

问题描述 投票:1回答:2

任何人都知道如何从单个产品页面上的其他选项卡隐藏产品尺寸,但仍显示重量值?

我搜索并查看此过滤器,但它隐藏了重量和尺寸。

add_filter( 'woocommerce_product_get_dimensions', '__return_false' );
wordpress templates woocommerce product dimensions
2个回答
4
投票

要仅隐藏尺寸(而不是重量),有两种方法可以使其工作。

1)使用钩子(这里是复合过滤钩):

查看在单个产品中显示维度的模板,您可以看到以下行:

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

然后,如果你看看WC_Product has_dimensions() method,你会看到这一行($thisWC_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; ?>

0
投票

如果其他一切都失败,你也可以使用css属性display:none

© www.soinside.com 2019 - 2024. All rights reserved.