Automattic\WooCommerce\Admin\API
ProductAttributeTerms::get_custom_attribute_values()
Query custom attribute values by slug.
Метод класса: ProductAttributeTerms{}
Хуков нет.
Возвращает
Массив
. Attribute values, formatted for response.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->get_custom_attribute_values( $slug );
- $slug(строка) (обязательный)
- Attribute slug.
Код ProductAttributeTerms::get_custom_attribute_values() ProductAttributeTerms::get custom attribute values WC 8.7.0
protected function get_custom_attribute_values( $slug ) { global $wpdb; if ( empty( $slug ) ) { return array(); } $attribute_values = array(); // Get the attribute properties. $attribute = $this->get_custom_attribute_by_slug( $slug ); if ( is_wp_error( $attribute ) ) { return $attribute; } // Find all attribute values assigned to products. $query_results = $wpdb->get_results( $wpdb->prepare( "SELECT meta_value, COUNT(meta_id) AS product_count FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value != '' GROUP BY meta_value", 'attribute_' . esc_sql( $slug ) ), OBJECT_K ); // Ensure all defined properties are in the response. $defined_values = wc_get_text_attributes( $attribute[ $slug ]['value'] ); foreach ( $defined_values as $defined_value ) { if ( array_key_exists( $defined_value, $query_results ) ) { continue; } $query_results[ $defined_value ] = (object) array( 'meta_value' => $defined_value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'product_count' => 0, ); } foreach ( $query_results as $term_value => $term ) { // Mimic the structure of a taxonomy-backed attribute values for response. $data = array( 'id' => $term_value, 'name' => $term_value, 'slug' => $term_value, 'description' => '', 'menu_order' => 0, 'count' => (int) $term->product_count, ); $response = rest_ensure_response( $data ); $response->add_links( array( 'collection' => array( 'href' => rest_url( $this->namespace . '/products/attributes/' . $slug . '/terms' ), ), ) ); $response = $this->prepare_response_for_collection( $response ); $attribute_values[ $term_value ] = $response; } return array_values( $attribute_values ); }