Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions

VariationSelectorAttribute::get_default_selected_attribute │ private │ WC 1.0

Get the default selected attribute.

Метод класса: VariationSelectorAttribute{}

Хуков нет.

Возвращает

string|null. The default selected attribute.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->get_default_selected_attribute( $attribute_slug, $attribute_terms ): ?string;
$attribute_slug(строка) (обязательный)
The attribute's slug.
$attribute_terms(массив) (обязательный)
The attribute's terms.

Код VariationSelectorAttribute::get_default_selected_attribute() WC 11.2.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;
}