Created
September 25, 2013 18:40
-
-
Save RadGH/6704079 to your computer and use it in GitHub Desktop.
For the WooCommerce plugin for WordPress, this function returns all variations of a product by referencing the 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 /* | |
Takes a product ID and returns an array that includes all types of variations of the product, and the attributes of that variation. | |
Variations are normally returned as a term object. They belong to the original product ID and the taxonomy name is equal to the attribute name. | |
If a custom variation is provided instead of a term object, the variation will simply be a string of the option's name. | |
Example return result is given below. This is a single product with one attribute and two different variations. | |
Array ( | |
[pa_oregon-training-classes] => Array ( | |
[attribute] => Array ( | |
[name] => pa_oregon-training-classes | |
[value] => | |
[position] => 2 | |
[is_visible] => 1 | |
[is_variation] => 1 | |
[is_taxonomy] => 1 | |
), | |
[variations] => Array ( | |
[09252013-6-00pm-salem] => stdClass Object ( | |
[term_id] => 30 | |
[name] => 05/27/13 -6:00pm- Salem, OR | |
[slug] => 05252013-6-00pm-salem | |
[term_group] => 0 | |
[term_taxonomy_id] => 30 | |
[taxonomy] => pa_oregon-training-classes | |
[description] => No materials required. Upon completion of class, student receives the certificate for the class | |
[parent] => 0 | |
[count] => 2 | |
), | |
[09252013-6-00pm-salem] => stdClass Object ( | |
[term_id] => 30 | |
[name] => 05/27/13 -6:00pm- Salem, OR | |
[slug] => 05252013-6-00pm-salem | |
[term_group] => 0 | |
[term_taxonomy_id] => 30 | |
[taxonomy] => pa_oregon-training-classes | |
[description] => No materials required. Upon completion of class, student receives the certificate for the class | |
[parent] => 0 | |
[count] => 2 | |
), | |
... | |
) | |
) | |
*/ | |
function woo_get_product_variations( $product_id ) { | |
$product_variations = array(); | |
// Make sure we are working with a product ID here | |
if ( is_object($product_id) ) { | |
if ( property_exists($product_id, 'id') ) $product_id = $product_id->id; | |
else if ( property_exists($product_id, 'ID') ) $product_id = $product_id->ID; | |
} | |
if ( !$product_id || get_post_type( $product_id ) !== 'product' ) return false; | |
// Get attributes of the base product. All variations are stored as an attribute in this custom post meta field. See: writepanel-product-type-variable.php | |
$attributes = maybe_unserialize( get_post_meta( $product_id, '_product_attributes', true ) ); | |
// Loop through all attributes in search of variations | |
foreach ( $attributes as $attribute ) { | |
// Only deal with variations | |
if ( ! $attribute['is_variation'] ) continue; | |
// Prepare a new variations array which we will add our individual variations to | |
if ( isset($attribute['name']) ) { | |
$variation = &$product_variations[ $attribute['name'] ]; | |
}else{ | |
$variation = &$product_variations[]; | |
} | |
$variation['attribute'] = $attribute; | |
$variation['variations'] = array(); | |
// Get terms for attribute taxonomy or value if its a custom attribute | |
if ( $attribute['is_taxonomy'] ) { | |
// Variation is a taxonomy, add each term to the attribute | |
$post_terms = wp_get_post_terms( $product_id, $attribute['name'] ); | |
foreach ( $post_terms as $term ) { | |
$variation['variations'][ $term->slug ] = $term; | |
} | |
} else { | |
// Variation is a custom attribute, separated by vertical pipes. Split the pipes and return them as a basic string. | |
$options = array_map( 'trim', explode( '|', $attribute['value'] ) ); | |
foreach ( $options as $option ) { | |
$variation['variations'][ sanitize_title( $option ) ] = $option; | |
} | |
} | |
} | |
return $product_variations; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment