• Resolved bond007f

    (@bond007f)


    Hi everyone,

    I am looking to hide a single product custom attribute when a specific category is attached to a product. I am thinking there may be some PHP code to do this. Something like this code here, but other then removing images, it can hide a single custom attribute

    /**
     * Remove product content based on category
     */
    add_action( 'wp', 'remove_product_content' );
    function remove_product_content() {
    	// If a product in the 'Cookware' category is being viewed...
    	if ( is_product() && has_term( 'Cookware', 'product_cat' ) ) {
    		//... Remove the images
    		remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
    		// For a full list of what can be removed please see woocommerce-hooks.php
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter bond007f

    (@bond007f)

    Maybe the remove action code could uncheck the Visible on the product page box on this one attribute somehow

    Can you send a screenshot of exactly you want hidden and if also a URL

    Thread Starter bond007f

    (@bond007f)

    ok so I have two images for you. In the first you will find the category Chandelier highlighted in yellow.
    http://2ndave.com/capture-4.png

    In the second image you will see that same Chandelier category on top and the attribute Item Min Length or Square: lower down the page highlighted.
    http://2ndave.com/capture-5.png

    What I want to do is set it up when the category is Chandelier Hide the attribute Item Min Length or Square: aka (pa_item-min-length-or-square) because that dimension is not needed for that category. Is this possible with php code put into functions.php

    I would send the url, but the site is currently in construction mode and can only be viewed with a login

    The first one you can maybe write an if statement to hide that cat and the second one I would maybe hide it with page id in css?

    Thread Starter bond007f

    (@bond007f)

    I have found the following code to hide an attribute, just need to figure out how to base it on category. Do you have any suggestions?

    		function mycode_hide_attributes_from_additional_info_tabs( $attributes, $product ) {
    	/**
    	 * Array of attributes to hide from the Additional Information
    	 * tab on single WooCommerce product pages.
    	 */
    			$hidden_attributes = [
    		'pa_item-min-length-or-square',
    		
    	];
    	foreach ( $hidden_attributes as $hidden_attribute ) {
    		if ( ! isset( $attributes[ $hidden_attribute ] ) ) {
    			continue;
    		}
    		$attribute = $attributes[ $hidden_attribute ];
    		$attribute->set_visible( false );
    	
    	}
    	return $attributes;
    }
    
    		
    add_filter( 'woocommerce_product_get_attributes', 'mycode_hide_attributes_from_additional_info_tabs', 20, 2 );
    Thread Starter bond007f

    (@bond007f)

    Got it working with the following code

    add_action( 'wp', 'remove_product_content' );
    function remove_product_content() {
    	// If a product in the 'Cookware' category is being viewed...
    	if ( has_term( array('CHANDEL-AIR', 'CHANDELIER','POT RACK','VANITY','BILLIARD/ISLAND' ), 'product_cat' ) ) {
    		
    		function mycode_hide_attributes_from_additional_info_tabs( $attributes, $product ) {
    	/**
    	 * Array of attributes to hide from the Additional Information
    	 * tab on single WooCommerce product pages.
    	 */
    			$hidden_attributes = [
    		'pa_item-min-length-or-square',
    		
    	];
    	foreach ( $hidden_attributes as $hidden_attribute ) {
    		if ( ! isset( $attributes[ $hidden_attribute ] ) ) {
    			continue;
    		}
    		$attribute = $attributes[ $hidden_attribute ];
    		$attribute->set_visible( false );
    	
    	}
    	
    	return $attributes;
    }
    
    add_filter( 'woocommerce_product_get_attributes', 'mycode_hide_attributes_from_additional_info_tabs', 20, 2 );
    }
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hide Attribute based on Category’ is closed to new replies.