Skip to content

Instantly share code, notes, and snippets.

@isabelc
Last active January 7, 2016 13:47
Show Gist options
  • Save isabelc/ff1fd41e4b9e556314da to your computer and use it in GitHub Desktop.
Save isabelc/ff1fd41e4b9e556314da to your computer and use it in GitHub Desktop.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment