quasel
1/7/2016 - 1:47 PM

Check if your WooCommerce custom product attributes are taxonomies. This will show custom product attributes on your product page above the

Check if your WooCommerce custom product attributes are taxonomies. This will show custom product attributes on your product page above the Add to Cart button. THIS WILL ALSO SHOW A NOTICE ON THE PRODUCT PAGE RIGHT UNDER THE ATTRIBUTES. THE NOTICE WILL EITHER SAY: "These ARE taxonomies." or "NOT a taxonomy."

/**
* Check if your WooCommerce custom product attributes are taxonomies.
*
* This will show custom product attributes on your product page
* above the Add to Cart button.
* 
* THIS WILL ALSO SHOW A NOTICE ON THE PRODUCT PAGE
* RIGHT UNDER THE ATTRIBUTES.
*
* THE NOTICE WILL EITHER SAY:
*
* "These ARE taxonomies."
*
* or
*
* "NOT a taxonomy."
*
*/

function isa_check_if_woocommerce_pa_is_taxonomy(){
  
    global $product;
    $attributes = $product->get_attributes();
  
    if ( ! $attributes ) {
        return;
    }
  
    $out = '';
  
    foreach ( $attributes as $attribute ) {

            // skip variations
            if ( $attribute['is_variation'] ) {
                continue;
            }
           
        if ( $attribute['is_taxonomy'] ) {

            $terms = wp_get_post_terms( $product->id, $attribute['name'], 'all' );
			
			// get the taxonomy
			$tax = $terms[0]->taxonomy;
			
			// get the tax object
			$tax_object = get_taxonomy($tax);
			
			// get tax label
			if ( isset ($tax_object->labels->name) ) {
				$tax_label = $tax_object->labels->name;
			} elseif ( isset( $tax_object->label ) ) {
				$tax_label = $tax_object->label;
			}
			
            foreach ( $terms as $term ) {
 
                $out .= $tax_label . ': ';
                $out .= $term->name . '<br />';
                 
            }
			
			/*
			***************************************
			
			Begin @test
			
			***************************************
			*/
              
			  
			$out .= '<div style="margin: 8px;padding:8px; color: #4F8A10;background-color: #DFF2BF">These ARE taxonomies.</div>';
			
			
			/*
			***************************************
			
			end @test
			
			***************************************
			*/
              
        } else {
            $out .= $attribute['name'] . ': ';
            $out .= $attribute['value'] . '<br />';
			
			/*
			***************************************
			
			Begin @test
			
			***************************************
			*/
              
			  
			$out .= '<div style="margin: 8px;padding:8px; color: #D8000C;background-color: #FFBABA">NOT a taxonomy.</div>';
			
			
			/*
			***************************************
			
			end @test
			
			***************************************
			*/
              
        }
    }
    echo $out;
}
   
add_action('woocommerce_single_product_summary', 'isa_check_if_woocommerce_pa_is_taxonomy', 25);