如何在自定义循环中获得 woocommerce 可变产品的最低和最高价格? [英] How can I get Min and Max price of a woocommerce variable product in Custom loop?

查看:21
本文介绍了如何在自定义循环中获得 woocommerce 可变产品的最低和最高价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Wordpress 中创建了一个自定义休息路线,它采用产品类别 ID,并将发送属于该类别的产品.

I've created a custom rest route in Wordpress which takes a product category id and will send products which are in that category.

我的问题是我无法找到在自定义循环中获取可变产品最低和最高价格的方法.

My Problem is I can't figure out a way to get variable product min and max prices inside my custom loop.

谁能帮我解决这个问题?谢谢

Can anyone help me with this issue? Thanks

我曾尝试使用 get_variation_prices() 获取变化价格,但似乎我遗漏了一些东西.

I've tried to get variation price using get_variation_prices() but it seems that I'm missing something.

add_action( 'rest_api_init' , 'wt_rest_api'); 
function wt_rest_api(){
   register_rest_route('wtrest','products',array(
           'methods'   => WP_REST_SERVER::READABLE,
           'callback'  => 'wtProductResults'
       )); 
} 

function wtProductResults($data){
    $products = new WP_Query([
           'post_type' => 'product',
           'tax_query' => array(
                               array(
                                   'taxonomy' => 'product_cat',
                                   'field' => 'term_id', //can be set to ID
                                   'terms' => $data['cat'] //if field is ID you can reference by cat/term number
                               )
                           )
        ]);

    $productsResults = [];
    global $woocommerce;
    global $product;
   $currency = get_woocommerce_currency_symbol();

    while($products->have_posts()){
        $products->the_post();
        $product_cat = get_term(  $data['cat'], 'product_cat', 'category', "OBJECT" );
        $regularPrice = get_post_meta( get_the_ID(), '_regular_price', true);
        $sale = get_post_meta( get_the_ID(), '_sale_price', true);
        $price = get_post_meta( get_the_ID(), '_price', true );
        array_push($productsResults , [
               'title' => get_the_title(),
               'productId' => get_the_id(),
               'permalink' => get_the_permalink(),
               'thumbnail' => get_the_post_thumbnail(),
               'excerpt' => get_the_excerpt(),
               'regularPrice' => $regularPrice,
               'price'     => $price,
               'salePrice' => $sale,
               'category' => $product_cat->name,
               'variationPrice' => get_variation_prices()//**Here is My problem**
            ]);
    }
    wp_reset_postdata();    
    return $productsResults;

}

这是我的代码,当我使用 get_variation_prices() 时,我的休息路线没有得到任何响应

Here is my Code and when I used get_variation_prices() I didn't get any response from my rest route

推荐答案

get_variation_prices() 函数是 WC_Product_Variable 类的一个方法,它专门用作一个可变的产品实例对象.它给出了所有变化价格的多维数组.

The function get_variation_prices() is a method of WC_Product_Variable Class and it works exclusively as a method on a variable product instance object. It gives a multi dimensional array of all variations prices.

要获得最小和最大变化价格,您必须使用 WC_Product_Variable 方法:

To get Min and Max variation prices, you have to use WC_Product_Variable methods:

  • get_variation_regular_price()get_variation_regular_price('max')
  • get_variation_sale_price()get_variation_sale_price('max')
  • get_variation_price()get_variation_price('max')
  • get_variation_regular_price() or get_variation_regular_price('max')
  • get_variation_sale_price() or get_variation_sale_price('max')
  • get_variation_price() or get_variation_price('max')

现在在您的代码中:

  • 您首先需要获取 WC_Product 实例对象.
  • 检查产品类型.
  • 删除 global $product; 因为它是空的而且没有用.
  • (根据您的要求,您可能需要进行其他更改)
  • you will need first to get the WC_Product instance object.
  • check products type.
  • remove global $product; as it's empty and not useful.
  • (you might need to make other changes, based on your requirements)

现在有多种查询产品的方式:

Now there is multiple ways to query products:

1) 使用 WP_Query (就像你实际做的那样):

function wtProductResults($data){
    global $woocommerce;
    
    $products = new WP_Query([
       'post_type' => 'product',
       'tax_query' => array( array(
            'taxonomy' => 'product_cat',
            'field' => 'term_id', //can be set to ID
            'terms' => $data['cat'] //if field is ID you can reference by cat/term number
        ) )
    ]);
    
    $productsResults = [];
    $currency        = get_woocommerce_currency_symbol();
   
    if ( $products->have_posts() ) :
    while ( $products->have_posts() ) : $products->the_post();
    
    $product_cat = get_term(  $data['cat'], 'product_cat', 'category', "OBJECT" );
        
    // Get an instance of the WC_Product object
    $product = wc_get_product( get_the_ID() );
    
    if( $product->is_type('variable') ) {
        // Min variation price
        $regularPriceMin = $product->get_variation_regular_price(); // Min regular price
        $salePriceMin    = $product->get_variation_sale_price(); // Min sale price
        $priceMin        = $product->get_variation_price(); // Min price
    
        // Max variation price
        $regularPriceMax = $product->get_variation_regular_price('max'); // Max regular price
        $salePriceMax    = $product->get_variation_sale_price('max'); // Max sale price
        $priceMax        = $product->get_variation_price('max'); // Max price
    
        // Multi dimensional array of all variations prices 
        $variationsPrices = $product->get_variation_prices(); 
        
        $regularPrice = $salePrice = $price = '';
        $variationPrice = [
            'min' => $product->get_variation_price(),
            'max' => $product->get_variation_price('max')
        ];
    } 
    // Other product types
    else {
        $regularPrice   = $product->get_regular_price();
        $salePrice      = $product->get_sale_price();
        $price          = $product->get_price(); 
        $variationPrice = ['min' => '', 'max' => ''];
    }
    
    array_push( $productsResults , [
       'title'          => get_the_title(),
       'productId'      => get_the_id(),
       'permalink'      => get_the_permalink(),
       'thumbnail'      => get_the_post_thumbnail(),
       'excerpt'        => get_the_excerpt(),
       'regularPrice'   => $regularPrice,
       'price'          => $price,
       'salePrice'      => $salePrice,
       'category'       => $product_cat->name,
       'variationPrice' => $variationPrice,
    ]);
    
    endwhile; 
    wp_reset_postdata();
    endif;
    
    return $productsResults;
}


2) 使用 WC_Product_Query 改为:

function wtProductResults($data){
    global $woocommerce;
    
    $products = wc_get_products( array(
        'status'    => 'publish',
        'limit'     => -1,
        'category'  => array($data['cat']),
    ) );
    
    $productsResults = [];
    $currency        = get_woocommerce_currency_symbol();
   
    if ( sizeof($products) > 0 ) :
    foreach ( $products as $product ) :
    
    $term_name = get_term( $data['cat'], 'product_cat' )->name;
    
    if( $product->is_type('variable') ) {
        // Min variation price
        $regularPriceMin = $product->get_variation_regular_price(); // Min regular price
        $salePriceMin    = $product->get_variation_sale_price(); // Min sale price
        $priceMin        = $product->get_variation_price(); // Min price
    
        // Max variation price
        $regularPriceMax = $product->get_variation_regular_price('max'); // Max regular price
        $salePriceMax    = $product->get_variation_sale_price('max'); // Max sale price
        $priceMax        = $product->get_variation_price('max'); // Max price
    
        // Multi dimensional array of all variations prices 
        $variationsPrices = $product->get_variation_prices(); 
        
        $regularPrice = $salePrice = $price = '';
        $variationPrice = [
            'min' => $product->get_variation_price(),
            'max' => $product->get_variation_price('max')
        ];
    } 
    // Other product types
    else {
        $regularPrice   = $product->get_regular_price();
        $salePrice      = $product->get_sale_price();
        $price          = $product->get_price(); 
        $variationPrice = ['min' => '', 'max' => ''];
    }
    
    array_push( $productsResults , [
       'title'          => $product->get_name(),
       'productId'      => $product->get_id(),
       'permalink'      => $product->get_permalink(),
       'thumbnail'      => $product->get_image(),
       'excerpt'        => $product->get_short_description(),
       'regularPrice'   => $regularPrice,
       'price'          => $price,
       'salePrice'      => $salePrice,
       'category'       => $term_name,
       'variationPrice' => $variationPrice,
    ]);
    
    endforeach; 
    endif;
    
    return $productsResults;
}

这篇关于如何在自定义循环中获得 woocommerce 可变产品的最低和最高价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆