Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions

VariationSelectorAttribute::get_default_selected_attributeprivateWC 1.0

Get the default selected attribute.

Method of the class: VariationSelectorAttribute{}

No Hooks.

Returns

String|null. The default selected attribute.

Usage

// private - for code of main (parent) class only
$result = $this->get_default_selected_attribute( $attribute_slug, $attribute_terms ): ?string;
$attribute_slug(string) (required)
The attribute's slug.
$attribute_terms(array) (required)
The attribute's terms.

VariationSelectorAttribute::get_default_selected_attribute() code WC 10.9.1

private function get_default_selected_attribute( string $attribute_slug, array $attribute_terms ): ?string {
	if ( isset( $_GET[ $attribute_slug ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$raw = wp_unslash( $_GET[ $attribute_slug ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		if ( is_string( $raw ) ) {
			$attribute_slug_from_request = sanitize_title( $raw );
			foreach ( $attribute_terms as $attribute_term ) {
				if ( sanitize_title( $attribute_term['value'] ) === $attribute_slug_from_request ) {
					return $attribute_term['value'];
				}
			}
		}
	} else {
		foreach ( $attribute_terms as $attribute_term ) {
			if ( $attribute_term['isSelected'] ) {
				return $attribute_term['value'];
			}
		}
	}

	return null;
}