woocommerce获取属性条款

问题描述:

在Woocommerce中,您可以添加全球产品属性和条款。例如:woocommerce获取属性条款

Size (attribute) 
small (term) 
medium (term) 
large (term) 

这是独立产品。然后,您可以从产品上的预定义属性中进行选择。

我需要使用php获取属性中的所有术语。因此,选择所需的属性,例如大小,然后返回包含[small,medium,large]的数组。

看起来很简单,但我找不到任何帮助。

有点混淆,特别是在浏览WooCommerce Docs时,因为绝对没有提及获取术语/属性列表。

属性保存为自定义分类,条款是分类术语。这意味着你可以使用原生的WordPress功能:Wordpress get_terms() Function Reference

通过点击WooCommerce的属性,你可以看看在URL中,你可以看到他们都前缀为“PA_”

这可能是你所需要的:

$terms = get_terms("pa_size"); 
foreach ($terms as $term) { 
echo "<option>" . $term->name . "</option>"; 
} 
+0

这是正确的答案。 – 2015-07-27 18:06:11

+0

我该如何计算它? – huykon225 2017-03-03 08:53:26

我用这个:

echo '<h1>variations</h1>'; 
mario($product->get_available_variations()); 
echo '<h1>Atributos</h1>'; 
mario($product->get_attributes()); 
echo '<h1>Poste Terms</h1>'; 
mario(wp_get_post_terms($post->ID, 'pa_color')); 


function mario($texto){ 
    echo '<pre>';var_dump($texto);echo '</pre>'; 
}; 

真有: “wp_get_post_terms($后> ID, 'pa_color')” 我只搜索一个词,b我们的想法是循环返回该函数的键['name']。

+0

谢谢你的回答,这真的很有帮助。 – 2016-09-13 06:20:41

我希望能够从后端获取所有已设置的属性,并将它们放在数组中以供我使用,我从类-wc-admin-attributes.php文件中取出了一些代码并修改了它为我的需求:

$attribute_taxonomies = wc_get_attribute_taxonomies(); 
$taxonomy_terms = array(); 

if ($attribute_taxonomies) : 
    foreach ($attribute_taxonomies as $tax) : 
    if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) : 
     $taxonomy_terms[$tax->attribute_name] = get_terms(wc_attribute_taxonomy_name($tax->attribute_name), 'orderby=name&hide_empty=0'); 
    endif; 
endforeach; 
endif; 

var_dump($taxonomy_terms); 

exit; 

通过所有的属性分类这将循环,检索每个条款,让你有项对象的数组为每个分类工作。

+0

感谢这段代码:) – jack 2017-02-06 19:10:07