T
T
trenton2021-06-17 03:34:27
WordPress
trenton, 2021-06-17 03:34:27

How to get min and max value of digital meta field but not prices?

This is generally a piece of a large filter, but for now I will post only a piece that I have not figured out, the rest is not on this topic.
Here at the price the minimum and maximum value is taken from the base in a piece where $sql.
What should be entered instead of price_meta.meta_value in order to get the minimum and maximum value of your meta field from the database in the same way? The input type is also number.
Or, as an option, attach to the default meta fields of woocommerce, like the length-width-height of the product. What should I enter instead of price_meta.meta_value for them? Where to get these values?
As I understand it, this is a direct sql query, only in code. I don't really understand sql.

protected function get_filtered_price() {
        global $wpdb;

        $args       = wc()->query->get_main_query()->query_vars;
        $tax_query  = isset( $args['tax_query'] ) ? $args['tax_query'] : array();
        $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();

        if ( ! is_post_type_archive( 'product' ) && ! empty( $args['taxonomy'] ) && ! empty( $args['term'] ) ) {
            $tax_query[] = array(
                'taxonomy' => $args['taxonomy'],
                'terms'    => array( $args['term'] ),
                'field'    => 'slug',
            );
        }

        foreach ( $meta_query + $tax_query as $key => $query ) {
            if ( ! empty( $query['price_filter'] ) || ! empty( $query['rating_filter'] ) ) {
                unset( $meta_query[ $key ] );
            }
        }

        $meta_query = new WP_Meta_Query( $meta_query );
        $tax_query  = new WP_Tax_Query( $tax_query );

        $meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
        $tax_query_sql  = $tax_query->get_sql( $wpdb->posts, 'ID' );

        $sql  = "SELECT min( FLOOR( price_meta.meta_value ) ) as min_price, max( CEILING( price_meta.meta_value ) ) as max_price FROM {$wpdb->posts} ";      
        $sql .= " LEFT JOIN {$wpdb->postmeta} as price_meta ON {$wpdb->posts}.ID = price_meta.post_id " . $tax_query_sql['join'] . $meta_query_sql['join'];
        $sql .= " 	WHERE {$wpdb->posts}.post_type IN ('" . implode( "','", array_map( 'esc_sql', apply_filters( 'woocommerce_price_filter_post_type', array( 'product' ) ) ) ) . "')
      AND {$wpdb->posts}.post_status = 'publish'
      AND price_meta.meta_key IN ('" . implode( "','", array_map( 'esc_sql', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) ) . "')
      AND price_meta.meta_value > '' ";
        $sql .= $tax_query_sql['where'] . $meta_query_sql['where'];

        $search = WC_Query::get_main_search_query_sql();
        if ( $search ) {
            $sql .= ' AND ' . $search;
        }

        return $wpdb->get_row( $sql ); // WPCS: unprepared SQL ok.
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2021-06-17
@pLavrenov

I may now offer an outdated solution, as it is taken from an old project. But working

function out_meta_value($meta, $post_type, $out = 'MAX') {
    global $wpdb;
    return $wpdb->get_var(
        $wpdb->prepare("
      SELECT {$out}( cast( meta_value as unsigned) ) FROM {$wpdb->postmeta} pm
      LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
      WHERE pm.meta_key = '%s'
      AND p.post_status = 'publish'
      AND p.post_type = '%s'",
            $meta,
            $post_type
        )
    );
}

$minPrice = out_meta_value('_new_price', 'product', 'MIN');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question