Skip to content

Instantly share code, notes, and snippets.

@deepzak deepzak/variable.php
Last active Aug 11, 2017

Embed
What would you like to do?
Woocommerce: set first stock variation as default variation
// Location: [your-theme]/woocommerce/single-product/add-to-cart
{ ..... }
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="value">
<?php
$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) : $product->get_available_variations()[0]['attributes'][ 'attribute_' . sanitize_title( $attribute_name ) ];
wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
echo end( $attribute_keys ) === $attribute_name ? '<a class="reset_variations" href="#">' . __( 'Clear selection', 'woocommerce' ) . '</a>' : '';
?>
</td>
</tr>
<?php endforeach;?>
{ ..... }
@shreyans94

This comment has been minimized.

Copy link

shreyans94 commented Aug 11, 2017

can you share updated version of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.