Skip to content

Instantly share code, notes, and snippets.

@carlosjunod
Last active April 4, 2018 17:19
Show Gist options
  • Save carlosjunod/b925b2860cd001e831c9ecb699db6ff7 to your computer and use it in GitHub Desktop.
Save carlosjunod/b925b2860cd001e831c9ecb699db6ff7 to your computer and use it in GitHub Desktop.
Drop down attributes woocommerce
<?php
// Get the attributes
$attributes = $product->get_attributes();
// Start the loop
$available_variations = $product->get_available_variations();
$product_id = get_the_ID();
if( count($available_variations) > 0 ){
$output = '<div class="product-variations-dropdown">
<select id="available-variations" class="" name="available_variations" data-id='. $product_id .'>';
$output .= '<option value="">'. __('Choose a variation') .'</option>';
foreach( $available_variations as $variation ){
$option_value = array();
foreach( $variation['attributes'] as $attribute => $term_slug ){
$taxonomy = str_replace( 'attribute_', '', $attribute );
$attribute_name = get_taxonomy( $taxonomy )->labels->singular_name; // Attribute name
$term_name = get_term_by( 'slug', $term_slug, $taxonomy )->name; // Attribute value term name
$option_value[] = $attribute_name . ': '.$term_name;
}
$option_value = implode( ' | ', $option_value );
$output .= '<option value="'.$variation['variation_id'].'">'.$option_value.'</option>';
}
$output .= '
</select>
</div>';
echo $output;
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment