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

标签 wordpress templates woocommerce product dimensions

有人知道如何在“单一产品”页面上的附加选项卡中隐藏产品尺寸,但仍显示重量值吗?

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

add_filter( 'woocommerce_product_get_dimensions', '__return_false' );

最佳答案

要仅隐藏尺寸(而不是重量),有2种方法使其发挥作用。

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

查看显示单个产品尺寸的模板,您可以看到这一行:

<?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();

所以当长度、高度和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/

相关文章:

C++继承/模板问题

c++ - 限制指针类型模板参数和覆盖模板化基类的虚方法

html - CSS 变化影响 2 个元素?

php - 通过 SSH 进行 MySQL 查询,在第 1 行给出错误 1064 (42000)

javascript - 触摸滚动在某些设备上不起作用

c++ - 在模板类之外的容器类型上编写模板化成员函数

Woocommerce:下订单,但稍后接受付款?

javascript - 在 Woocommerce 的 Adword 转换代码中添加订单数据

php - wpdb::prepare() 缺少参数 2

php - 在 Woocommerce 中获取自定义产品属性