Created
January 2, 2019 11:53
-
-
Save yogeshdubey2006/520c85e4359258b8bad926b2a3d7aad6 to your computer and use it in GitHub Desktop.
WooCommerce - Get values of product custom attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* When we make custom product attribute in WooCommerce, they are registered as a custom taxonomy. | |
* So we can use WordPress function get_the_terms() to retrieve them. | |
* When register these custom taxonomy, WooCommerce adds a prefix of pa_ to our custom product attribute. | |
* So if we create an attribute called Height, who's slug is 'height', then the custom taxonomy would be 'pa_height'. | |
* Now we are going to retrieve all terms in this attribute (height) of a product. | |
*/ | |
$pa_height = get_the_terms( $product->id, 'pa_height'); | |
if ( ! empty( $pa_height ) && ! is_wp_error( $pa_height ) ) { | |
foreach ( $pa_height as $pa_height_item ) { | |
echo $pa_height_item->name; | |
} | |
} ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment