wordpress 根据 Woocommerce 中的 ID 获取产品的所有产品变体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31232840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:07:14  来源:igfitidea点击:

Get all product variations of a product given its ID in Woocommerce

wordpresswoocommerce

提问by qbr

I have a custom page where I'm trying to list every products in the store along with their variations.

我有一个自定义页面,我试图在其中列出商店中的所有产品及其变体。

Also, I'm trying to list the variations' prices sorted by the product attribute with slug 'size'

此外,我正在尝试列出按产品属性排序的变体价格,并带有 slug 'size'

For testing, I'm trying to get the variations of a single product with the ID 381 My code yet is

为了进行测试,我正在尝试获取 ID 为 381 我的代码的单个产品的变体

    $handle=new WC_Product('381');
    $variations1=$handle->get_avaialable_variations();
    foreach ($variations1 as $key => $value) {
            echo '<option  value="'.$value['variation_id'].'">'.implode('/',$value['attributes']).'-'.$value['price_html'].'</option>';

    }

But the error I'm getting is

但我得到的错误是

PHP Fatal error:  Call to undefined method WC_Product::get_avaialable_variations() 

I tried using

我尝试使用

$handle=new WC_Product_Variable('381');

instead of

代替

 $handle=new WC_Product('381'); 

But the error is the same.

但错误是一样的。

Any help here?

这里有什么帮助吗?

回答by WisdmLabs

Try this code.

试试这个代码。

    $handle=new WC_Product_Variable('12');
        $variations1=$handle->get_children();
        foreach ($variations1 as $value) {
        $single_variation=new WC_Product_Variation($value);
            echo '<option  value="'.$value.'">'.implode(" / ", $single_variation->get_variation_attributes()).'-'.get_woocommerce_currency_symbol().$single_variation->price.'</option>';
}

Note: Use this $single_variation->get_price_html() but its outputs with html span tag which results in getting hidden in option tags.

注意:使用这个 $single_variation->get_price_html() 但它的输出带有 html span 标签,这导致隐藏在选项标签中。

Tested the above code and the results are as follows.

测试了上面的代码,结果如下。

Let me know if that worked for you too.

让我知道这是否也对你有用。

enter image description here

在此处输入图片说明

回答by Anthony

You had a typo in your code - get_avaialable_variations

你的代码有错别字 - get_avaialable_variations

It should be get_available_variations

它应该是 get_available_variations

回答by R T

function get_variation_data_from_variation_id($item_id) {

    $_product = new WC_Product_Variation($item_id);
    $variation_data = $_product->get_variation_attributes(); // variation data in array
    $variation_detail = woocommerce_get_formatted_variation($variation_data, true);  // this will give all variation detail in one line
    // $variation_detail = woocommerce_get_formatted_variation( $variation_data, false);  // this will give all variation detail one by one
    return $variation_data; // $variation_detail will return string containing variation detail which can be used to print on website
    // return $variation_data; // $variation_data will return only the data which can be used to store variation data
}

I found this before on stackoverflow couldn't remember the link (please edit if find the link to the answer with this method). it shows the variation data for output. the method takes variation id as perameter.

我之前在 stackoverflow 上发现了这个,无法记住链接(如果使用此方法找到答案的链接,请进行编辑)。它显示了输出的变化数据。该方法以变异 id 作为参数。

output:

输出

enter image description here.

在此处输入图片说明.