Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerrgg/c4c88c03fa6b00197da2881e4871e74a to your computer and use it in GitHub Desktop.
Save gerrgg/c4c88c03fa6b00197da2881e4871e74a to your computer and use it in GitHub Desktop.
<?php
function wcfbt_get_variation_dropdown( $product_id ){
/**
* Create a inline dropdown for selecting available product variations
*/
$product = wc_get_product( $product_id );
// if product exists and has available variations to list.
if( $product && ! empty( $product->get_variation_attributes() ) ){
foreach ( $product->get_variation_attributes() as $attribute_name => $options ) : ?>
<?php
echo '<strong>' . wc_attribute_label( $attribute_name ) . ': </strong>';
$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
?>
<?php endforeach;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment