pha*_*ist 16 attributes product e-commerce woocommerce
在Woocommerce中,您可以添加全局产品属性和术语.例如:
Size (attribute)
small (term)
medium (term)
large (term)
Run Code Online (Sandbox Code Playgroud)
这与产品无关.然后,您可以从产品上的预定义属性中进行选择.
我需要使用php获取属性中的所有术语.因此,选择所需的属性,例如大小,然后返回包含的数组[small,medium,large]
.
看起来很简单,但我找不到任何帮助.
小智 27
有点混乱,特别是在浏览WooCommerce Docs时,因为绝对没有提到获得术语/属性的列表.
属性保存为自定义分类,术语是分类术语.这意味着您可以使用本机Wordpress函数:Wordpress get_terms()函数参考
通过单击WooCommerce中的属性,您可以查看URL,并且可以看到它们都以"pa_"为前缀
这可能是你需要的:
$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<option>" . $term->name . "</option>";
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*ick 11
我希望能够从设置的后端获取所有不同的属性,并将它们放在一个数组中供我使用,我从class-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;
Run Code Online (Sandbox Code Playgroud)
这将循环遍历所有属性分类法,检索每个属性的术语,为您提供一组术语对象,以便为每个分类法使用.
小智 5
我用这个:
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>';
};
Run Code Online (Sandbox Code Playgroud)
实际上:“wp_get_post_terms($ post-> ID,'pa_color')”我只搜索一个术语,但想法是循环查找返回该函数的键['name']。
从 4.5.0 开始,分类法应该通过 $args 数组中的 'taxonomy' 参数传递:
$terms = get_terms( array(
'taxonomy' => 'pa_taxonyname',
'hide_empty' => false,
) );
Run Code Online (Sandbox Code Playgroud)
例如,如果分类 slug 是“日期”,那么分类将是“pa_date”。您还可以将鼠标悬停在属性名称上,然后在浏览器底部查看分类名称。
我希望它有帮助!
归档时间: |
|
查看次数: |
37101 次 |
最近记录: |