Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions
VariationSelectorAttribute::get_filtered_attribute_terms
Build filtered attribute term options for the variation selector.
Method of the class: VariationSelectorAttribute{}
No Hooks.
Returns
Array. Attribute terms, or empty string when none match.
Usage
// private - for code of main (parent) class only $result = $this->get_filtered_attribute_terms( $attribute_name, $product_attribute_terms, $available_values );
- $attribute_name(string) (required)
- Product attribute name.
- $product_attribute_terms(array) (required)
- Custom attribute terms when not a taxonomy.
- $available_values(array) (required)
- Available variation values for this attribute (value => true).
VariationSelectorAttribute::get_filtered_attribute_terms() VariationSelectorAttribute::get filtered attribute terms code WC 10.9.1
private function get_filtered_attribute_terms( string $attribute_name, array $product_attribute_terms, array $available_values ) {
global $product;
$selected_attribute = $product->get_variation_default_attribute( $attribute_name );
$terms = taxonomy_exists( $attribute_name )
? wc_get_product_terms( $product->get_id(), $attribute_name, array( 'fields' => 'all' ) )
: $product_attribute_terms;
$attribute_terms = array();
if ( ! empty( $available_values ) ) {
$allows_any_value = isset( $available_values[''] );
foreach ( $terms as $term ) {
$option = $this->map_term_to_option( $term, $attribute_name, $product, $selected_attribute );
if ( ! isset( $option['value'] ) ) {
continue;
}
if ( $allows_any_value || isset( $available_values[ $option['value'] ] ) ) {
$attribute_terms[] = $option;
}
}
}
return $attribute_terms;
}