Last active
June 23, 2020 17:09
-
-
Save raazon/bedba6560393c8ebb5c78f45656a602d to your computer and use it in GitHub Desktop.
WooCommerce get variation details by product id
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 | |
| $product_id = 10; | |
| $product = wc_get_product($product_id); | |
| $variations = $product->get_available_variations(); | |
| $variation_names = array(); | |
| foreach ( $variations as $variation ) { | |
| // Get attribute taxonomies | |
| $taxonomies = array_keys($variation['attributes']); | |
| // Loop through variation taxonomies to get variation name and slug | |
| foreach ($taxonomies as $tax) { | |
| $get_term_tax = str_replace('attribute_', '', $tax); | |
| $meta = get_post_meta( $variation['variation_id'], $tax, true ); | |
| $term = get_term_by( 'slug', $meta, $get_term_tax ); | |
| $var_name = $term->name; | |
| $variation_names[] = $var_name; | |
| } | |
| var_dump($variation['variation_id']); | |
| var_dump($variation['display_regular_price']); | |
| var_dump($variation['price_html']); | |
| var_dump($variation['variation_description']); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment