wc_dropdown_variation_attribute_options()WC 2.4.0

Output a list of variation attributes for use in the cart forms.

Return

null. Nothing (null).

Usage Template

<form>
	<?php

	wc_dropdown_variation_attribute_options([
		'options'   => $options,
		'attribute' => $attribute_name,
		'product'   => $product,
		'selected'  => $selected
	]);

	?>
</form>

Usage

wc_dropdown_variation_attribute_options( $args );
$args(array)

Parameters (arguments). Possible array keys:

  • options(array)
    Array with arbitrary options.
    Default: false

  • attribute(array)
    Name of the attribute for which you want to generate options.
    Default: false

  • product(object)
    Woocommerce product object.
    Default: false

  • selected(array)
    Selected option.
    Default: false

  • name(string)
    Attribute taxonomy name.
    Default: ''

  • id(string)
    The value for the id attribute of the select tag.
    Default value: ''

  • class(string)
    Value for the class attribute of the select tag.
    Default value: ''

  • show_option_none(string)
    Array with arbitrary options.
    Default: __( 'Choose an option', 'woocommerce' )

Default: array()

Examples

0

#1 A stripped down version of the Choose an option template

View file code `/templates/single-product/add-to-cart/variable.php'

<form class="variations_form cart"
	  action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>"
	  method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>"
	  data-product_variations="<?php echo htmlspecialchars( wp_json_encode( $available_variations ) ); // WPCS: XSS ok. ?>"
>
	<table class="variations" cellspacing="0">
		<tbody>
		<?php foreach ( $attributes as $attribute_name => $options ) : ?>
			<tr>
				<td class="label">
					<label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo esc_html( wc_attribute_label( $attribute_name ) ); ?></label>
				</td>
				<td class="value">
					<?php
					$selected = isset( $_REQUEST[ 'attribute_' . $attribute_name ] )
						? wc_clean( urldecode( wp_unslash( $_REQUEST[ 'attribute_' . $attribute_name ] ) ) )
						: $product->get_variation_default_attribute( $attribute_name ); // WPCS: input var ok, CSRF ok, sanitization ok.

					wc_dropdown_variation_attribute_options( array(
						'options'   => $options,
						'attribute' => $attribute_name,
						'product'   => $product,
						'selected'  => $selected,
					) );

					echo end( $attribute_keys ) === $attribute_name
						? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) )
						: '';
					?>
				</td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>
</form>

Changelog

Since 2.4.0 Introduced.

wc_dropdown_variation_attribute_options() code WC 8.6.1

function wc_dropdown_variation_attribute_options( $args = array() ) {
	$args = wp_parse_args(
		apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ),
		array(
			'options'          => false,
			'attribute'        => false,
			'product'          => false,
			'selected'         => false,
			'required'         => false,
			'name'             => '',
			'id'               => '',
			'class'            => '',
			'show_option_none' => __( 'Choose an option', 'woocommerce' ),
		)
	);

	// Get selected value.
	if ( false === $args['selected'] && $args['attribute'] && $args['product'] instanceof WC_Product ) {
		$selected_key = 'attribute_' . sanitize_title( $args['attribute'] );
		// phpcs:disable WordPress.Security.NonceVerification.Recommended
		$args['selected'] = isset( $_REQUEST[ $selected_key ] ) ? wc_clean( wp_unslash( $_REQUEST[ $selected_key ] ) ) : $args['product']->get_variation_default_attribute( $args['attribute'] );
		// phpcs:enable WordPress.Security.NonceVerification.Recommended
	}

	$options               = $args['options'];
	$product               = $args['product'];
	$attribute             = $args['attribute'];
	$name                  = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
	$id                    = $args['id'] ? $args['id'] : sanitize_title( $attribute );
	$class                 = $args['class'];
	$required              = (bool) $args['required'];
	$show_option_none      = (bool) $args['show_option_none'];
	$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' ); // We'll do our best to hide the placeholder, but we'll need to show something when resetting options.

	if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
		$attributes = $product->get_variation_attributes();
		$options    = $attributes[ $attribute ];
	}

	$html  = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '"' . ( $required ? ' required' : '' ) . '>';
	$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';

	if ( ! empty( $options ) ) {
		if ( $product && taxonomy_exists( $attribute ) ) {
			// Get terms if this is a taxonomy - ordered. We need the names too.
			$terms = wc_get_product_terms(
				$product->get_id(),
				$attribute,
				array(
					'fields' => 'all',
				)
			);

			foreach ( $terms as $term ) {
				if ( in_array( $term->slug, $options, true ) ) {
					$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</option>';
				}
			}
		} else {
			foreach ( $options as $option ) {
				// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
				$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
				$html    .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option, null, $attribute, $product ) ) . '</option>';
			}
		}
	}

	$html .= '</select>';

	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
}