romu
romu

Reputation: 177

Hide product attributes from additional information tab in WooCommerce

How to hide certain custom product attributes on single product pages additional informations tab?

Note: I don´t want to hide everything, just specific attributes.

For e.g i would like to hide "pa_size" to name one it it.

Only found this one, but its for a products weight.

add_filter( 'woocommerce_product_get_weight' , '__return_false' );

Upvotes: 7

Views: 16518

Answers (4)

factor11
factor11

Reputation: 1

It may no longer be relevant. BUT this snippet added to the Code Snippets or function.php plugin solves the problem:

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;
}

To register attribute names and use them, you need to add to the snippet:

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

And enable debugging in 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 );

After enabling WP_DEBUG_LOG, errors, warnings and our attributes output will be logged to the debug.log file, which is located in the /wp-content/ folder.

Upvotes: 0

Julian Wittorf
Julian Wittorf

Reputation: 51

Using the functions.php can cause problems with shipping, see here: https://github.com/woocommerce/woocommerce/issues/5985#issuecomment-322541850

Simply copy the wp-content/plugins/woocommerce/templates/single-product/product-attributes.php to wp-content/themes/YOUR_CHILD_THEME/woocommerce/single-product/product-attributes.php and add an if to check for the attribute. (As LoicTheAztec mentioned in #3)

This is from 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>

Upvotes: 1

ZeroGravity
ZeroGravity

Reputation: 79

I was looking for an answer for the same/similar issue, wanting to remove the additional information tab. I came across this post using the woocommerce_product_tabs filter

I added it to functions.php and the additional information tab is no longer added to the page.

Upvotes: 1

LoicTheAztec
LoicTheAztec

Reputation: 254363

For all custom product attributes you can hide them from additional information tab just deselecting the option "Visible on the product page" under product settings > Attributes tab:

enter image description here

1) To remove the product dimensions, you can disable that with the following code:

add_filter( 'woocommerce_product_get_dimensions', '__return_false' );

2) To remove everything from the tab ( weight, dimensions and custom attributes) use this:

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

3) To fine tune what you want to display:

You can override single-product/product-attributes.php template via your active child theme (or active theme) that displays everything in this product tab.

So you can remove any html block, that displays those details, or customize it…


Official documentation: Template structure & Overriding templates via a theme

Upvotes: 8

Related Questions