questions

Price of most expensive woocomerce product in admin

Rate this post

Im trying to get price(or id) of the most expensive product while beeing in woocomerce admin panel. With the code below i keep getting infinite loop, eventhough im not sure if this is correct way to do it. Tried many of different part of functions form stack but it didnt help me.

unction test() {

$args = array(
    'category' => array( 'blackcat' ),
    'orderby'  => 'name',
);
$products = wc_get_products( $args );
}

add_action( 'pre_get_posts', 'test' );

$query = array(
    'limit' => 1,
    'post_type'=> 'product',
    'orderby' => 'price',
    'order' => 'ASC',
    
);
$the_query= new WP_Query($query);


    if ( $the_query->have_posts() ) {
    
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        
        }
    
     } else {
         // no posts found
     }
     // Restore original Post Data 
     wp_reset_postdata();

   }
    
  }

}

add_action( 'parse_query', 'apply_my_custom_product_filters' );

Both returns Allowed memory size of ….. bytes exhausted in /wp-includes/class-wp-query.php

 

✔️Solution:

Figured it out. I was hooking to wrong place. Sould have use ‘restrict_manage_posts’

 add_action('restrict_manage_posts', 'yet', 10);

 function yet(){
 $args = array(
        'post_type' => 'product',
        'posts_per_page' => '1',
        'orderby' => 'price',
        'order' => 'DESC',
        
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();

 }

Leave a Reply

Your email address will not be published.

Back to top button