发布于 2025-02-13 05:56:18 字数 1043 浏览 0 评论 0原文

大家好,我正在尝试获得创建功能以获取产品的最小值& WooCommerce查询中的最高价格。 问题是我的查询没有获得价格的最低产品

产品的示例:

  1. 200 USD
  2. 300 USD出售
  3. 400 USD
  4. 500 USD
  5. 600 USD

,其结果是具有400美元的产品,必须是200不是400。

我的查询:

$args = array(
  'post_type' => 'product',
  'post_status' => 'publish',
  'posts_per_page' => 1,
  'ignore_stickie_posts' => true,
  'fields' => 'ids',
  'orderby' => 'price',
  'order' => 'DESC',
  
  );
 $products_price_min_args = get_posts($args);

$string_id = implode(",", $products_price_min_args);
$product = wc_get_product( $string_id );

if($product) {
  if ( $product->is_type('variable') ){
    $price_min = $product->get_variation_price('min'); // Float number
  }
  elseif ( $product->is_type('simple') ){
    if( $product->is_on_sale() ) {
      $price_min = $product->get_sale_price();         
    }
    else{
      $price_min = $product->get_price(); 
    }
  }
  return $price_min;
}

hello guys I'm trying to get create a function to get the product has the most min & max Price in the woocommerce query.
problem is my query not get the lowest product that has price

here example of products:

  1. 200 USD
  2. 300 USD ON SALE
  3. 400 USD
  4. 500 USD
  5. 600 USD

and the result is the product with 400 USD, and it must be 200 not 400.

my query :

$args = array(
  'post_type' => 'product',
  'post_status' => 'publish',
  'posts_per_page' => 1,
  'ignore_stickie_posts' => true,
  'fields' => 'ids',
  'orderby' => 'price',
  'order' => 'DESC',
  
  );
 $products_price_min_args = get_posts($args);

$string_id = implode(",", $products_price_min_args);
$product = wc_get_product( $string_id );

if($product) {
  if ( $product->is_type('variable') ){
    $price_min = $product->get_variation_price('min'); // Float number
  }
  elseif ( $product->is_type('simple') ){
    if( $product->is_on_sale() ) {
      $price_min = $product->get_sale_price();         
    }
    else{
      $price_min = $product->get_price(); 
    }
  }
  return $price_min;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2025-02-20 05:56:18

WooCommerce产品价格存储在此表中-WP_WC_PRODUCT_META_LOOKUP,

因此,您必须从该表中查询数据并获取最小价格。当您使用get_posts时,它正在从默认的WordPress邮局表中获取产品,该表未存储产品的价格。

这是从帖子表中获取产品最小价格的更新代码 -

$args = array(
      'post_type' => 'product',
      'post_status' => 'publish',
      'ignore_stickie_posts' => true,
      'fields' => 'ids',
    );
    
    $products = get_posts($args);
    
    $all_product_prices = array();
    
    foreach($products as $product_id) {
        
        $product = wc_get_product( $product_id );
        
        if ( $product->is_type('variable') ){
            $price_min = $product->get_variation_price('min'); // Float number
          }
          elseif ( $product->is_type('simple') ){
            if( $product->is_on_sale() ) {
              $price_min = $product->get_sale_price();         
            }
            else{
              $price_min = $product->get_price(); 
            }
          }
        
        array_push($all_product_prices, $price_min);
        
    }
    
    $price = min($all_product_prices);
    
    return $price;

The Woocommerce product prices are stored in this table - wp_wc_product_meta_lookup

So, you have to query the data from this table and get the min price. As you are using get_posts so it is getting the product from the default WordPress posts table where the price for the product isn't stored.

Here is the updated code to fetch the min price of the product from the posts meta table -

$args = array(
      'post_type' => 'product',
      'post_status' => 'publish',
      'ignore_stickie_posts' => true,
      'fields' => 'ids',
    );
    
    $products = get_posts($args);
    
    $all_product_prices = array();
    
    foreach($products as $product_id) {
        
        $product = wc_get_product( $product_id );
        
        if ( $product->is_type('variable') ){
            $price_min = $product->get_variation_price('min'); // Float number
          }
          elseif ( $product->is_type('simple') ){
            if( $product->is_on_sale() ) {
              $price_min = $product->get_sale_price();         
            }
            else{
              $price_min = $product->get_price(); 
            }
          }
        
        array_push($all_product_prices, $price_min);
        
    }
    
    $price = min($all_product_prices);
    
    return $price;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文