Skip to content

Instantly share code, notes, and snippets.

@AkremBelkahla
AkremBelkahla / woocommerce.php
Created October 25, 2020 17:21 — forked from marcosnakamine/woocommerce.php
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>