【问题标题】:Price of most expensive woocomerce product in admin管理员中最昂贵的 woocommerce 产品的价格
【发布时间】:2021-03-05 07:21:04
【问题描述】:

我试图在 woocomerce 管理面板中获取最昂贵产品的价格(或 ID)。使用下面的代码,我不断获得无限循环,尽管我不确定这是否是正确的方法。尝试了许多不同部分的函数形式堆栈,但它没有帮助我。

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' );

两者都返回 /wp-includes/class-wp-query.php 中耗尽的.....字节的允许内存大小

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    想通了。我挂错了地方。应该使用“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();
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2020-05-20
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多