Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions
VariationSelectorAttributeOptions::get_default_selected_attribute
Get the default selected attribute.
Method of the class: VariationSelectorAttributeOptions{}
No Hooks.
Returns
String|null. The default selected attribute.
Usage
// protected - for code of main (parent) or child class $result = $this->get_default_selected_attribute( $attribute_slug, $attribute_terms );
- $attribute_slug(string) (required)
- The attribute's slug.
- $attribute_terms(array) (required)
- The attribute's terms.
VariationSelectorAttributeOptions::get_default_selected_attribute() VariationSelectorAttributeOptions::get default selected attribute code WC 10.7.0
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;
}