Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions

VariationSelectorAttributeOptions::get_default_selected_attributeprotectedWC 1.0

Get the default selected attribute.

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

Хуков нет.

Возвращает

Строку|null. The default selected attribute.

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

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

Код VariationSelectorAttributeOptions::get_default_selected_attribute() WC 10.3.6

protected function get_default_selected_attribute( $attribute_slug, $attribute_terms ) {
	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;
}