在 WooCommerce 的附加信息选项卡中隐藏产品属性

问题描述 投票:0回答:4

如何隐藏单个产品页面附加信息选项卡上的某些自定义产品属性?

注意:我不想隐藏所有内容,只是隐藏特定属性。

例如,我想隐藏

"pa_size"
来命名它。

只找到这个,但它是产品重量。

add_filter( 'woocommerce_product_get_weight' , '__return_false' );
php wordpress woocommerce product hook-woocommerce
4个回答
8
投票

对于所有自定义产品属性,您可以从附加信息选项卡中隐藏它们,只需在产品设置 > 属性选项卡下取消选择选项 “在产品页面上可见”

enter image description here

1) 要删除产品尺寸,您可以使用以下代码禁用它:

add_filter( 'woocommerce_product_get_dimensions', '__return_false' );

2) 要从选项卡中删除所有内容(重量、尺寸和自定义属性),请使用以下命令:

remove_action( 'woocommerce_product_additional_information', 'wc_display_product_attributes', 10 );

3) 微调要显示的内容:

您可以通过活动子主题

(或活动主题)
覆盖 single-product/product-attributes.php 模板,以显示此产品选项卡中的所有内容。

因此,您可以删除任何显示这些详细信息的 html 块,或对其进行自定义......


官方文档:模板结构和通过主题覆盖模板


1
投票

我正在寻找相同/类似问题的答案,想要删除附加信息选项卡。我发现这篇文章使用 woocommerce_product_tabs 过滤器

我将其添加到functions.php中,附加信息选项卡不再添加到页面中。


1
投票

使用

functions.php
可能会导致运输问题,请参阅此处:https://github.com/woocommerce/woocommerce/issues/5985#issuecomment-322541850

只需将

wp-content/plugins/woocommerce/templates/single-product/product-attributes.php
复制到
wp-content/themes/YOUR_CHILD_THEME/woocommerce/single-product/product-attributes.php
并添加
if
即可检查属性。 (正如 LoicTheAztec 在 #3 中提到的)

这是来自 WooCommerce 4.4.1:

<?php
/**
 * Product attributes
 *
 * Used by list_attributes() in the products class.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/product-attributes.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates
 * @version 3.6.0
 */

defined( 'ABSPATH' ) || exit;

if ( ! $product_attributes ) {
    return;
}
?>
<table class="woocommerce-product-attributes shop_attributes">
    <?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
        <?php // Hide weight attribute in frontend ?>
        <?php if ( esc_attr( $product_attribute_key ) !== 'weight' ): ?>
            <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
                <th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
                <td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
            </tr>
        <?php endif; ?>
    <?php endforeach; ?>
</table>

0
投票

它可能不再相关。但是将此代码段添加到代码片段或 function.php 插件解决了问题:

add_filter( 'woocommerce_product_get_attributes', 'hide_custom_product_attributes', 10, 2 );

function hide_custom_product_attributes( $attributes, $product ) {
    // Array of attributes to hide
    $attributes_to_hide = array(
        'pa_atr1', // name of the first attribute
        'pa_atr2' // name of the second attribute
    );

    // Delete the specified attributes
    foreach ( $attributes_to_hide as $attribute_key ) {
        if ( isset( $attributes[ $attribute_key ] ) ) {
            unset( $attributes[ $attribute_key ] );
        }
    }

    return $attributes;
}

要注册属性名称并使用它们,您需要添加到代码片段中:

add_action('wp_footer', function() {
    global $product;
        if ($product) {
            $attributes = $product->get_attributes();
            foreach ($attributes as $slug => $attribute) {
                error_log('Атрибут: ' . $slug);
            }
        }
});

并在 wp-config 中启用调试

// Enable debug mode 
define( 'WP_DEBUG', true ); 
// Log errors to debug.log file 
define( 'WP_DEBUG_LOG', true ); 
// Disable display of errors on the site (for security) 
define( 'WP_DEBUG_DISPLAY', false );

启用 WP_DEBUG_LOG 后,错误、警告和我们的属性输出将被记录到位于 /wp-content/ 文件夹中的 debug.log 文件中。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.