Skip to content

Instantly share code, notes, and snippets.

@marcosnakamine
Last active October 25, 2020 17:21
Show Gist options
  • Save marcosnakamine/78b6493ffe3fcb689558bfc1a449f2b0 to your computer and use it in GitHub Desktop.
Save marcosnakamine/78b6493ffe3fcb689558bfc1a449f2b0 to your computer and use it in GitHub Desktop.
WooCommerce - Get variable product attributes
<?php $attributes = $product->get_attributes() // GET ALL ATRIBUTES ?>
<?php foreach( $attributes as $key => $value ): ?>
<?php $attribute_name = preg_replace( '/pa_/', '', $key ) // GET ATTRIBUTE NAME ?>
<label>
<select name="attribute_pa_<?php echo $attribute_name ?>" id="attribute_pa_<?php echo $attribute_name ?>">
<option value=""><?php echo $attribute_name ?></option>
<?php $attribute_name = wc_get_product_terms( get_the_ID(), $key ) // GET ATTRIBUTE NAME ?>
<?php $attribute_slug = wc_get_product_terms( get_the_ID(), $key, array( 'fields' => 'slugs' ) ) // GET ATTRIBUTE SLUG ?>
<?php for ( $i=0; $i<count( $attribute_name ); $i++ ): // array_slice BECAUSE ARRAY INDEX IS NOT SEQUENCIAL ?>
<option value="<?php $slug = array_slice( $attribute_slug, $i, 1 ); echo $slug[0]; ?>"><?php $name = array_slice( $attribute_name, $i, 1 ); echo $name[0]; ?></option>
<?php endfor ?>
</select>
</label>
<?php endforeach ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment