获取所有的“活动"消息归因于WooCommerce产品附加的分类法? [英] Get all "active" attributes taxonomies attached to products in WooCommerce?
问题描述
我想获取产品所附的所有活动"属性列表,
I want to get all "active" attributes list that attached to the products,
因此,如果属性存在,但不附加到任何产品上,则不会显示.
so if the attribute exists, but don't attach to any products don't display.
我可以将所有属性显示为下拉列表,如下所示:
I can display all attributes as dropdown like this:
$attributes = wc_get_attribute_taxonomies();
if($attributes) {
echo '<select name="all-attributes" id="all-attributes">';
foreach ( $attributes as $attribute ) {
echo '<option value="' . $attribute->attribute_name . '">' . $attribute->attribute_label . '</option>';
}
echo '</select>';
}
但是通过这种方式,我可以获取所有属性,即使未连接非活动属性也是如此.
But this way I'm getting all attributes, even non active attributes is not attached.
如何在WooCommerce中获得附加到产品的所有活动产品属性分类法?
How to get all active product attributes taxonomies attached to products in WooCommerce?
推荐答案
要获得所有活动产品属性分类法(至少与产品关联),您将需要一个自定义简单sql查询,如下所示(嵌入php函数):
To get all active product attributes taxonomies (attached a least to a product) you will need a custom simple sql query as follow (embedded in a php function):
function wc_get_active_attribute_taxonomies() {
global $wpdb;
return $wpdb->get_results( "
SELECT DISTINCT wat.*, tt.taxonomy
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies wat
INNER JOIN {$wpdb->prefix}term_taxonomy tt
ON tt.taxonomy = CONCAT('pa_', wat.attribute_name)
INNER JOIN {$wpdb->prefix}term_relationships tr
ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tt.count > 0
" );
}
代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
用法(根据您的代码):
USAGE (based on your code):
只需替换:
$attributes = wc_get_attribute_taxonomies();
作者:
$attributes = wc_get_active_attribute_taxonomies();
注意::该查询输出另外包含分类法"参数.
Note: This query output, includes additionally the "taxonomy" argument.
这篇关于获取所有的“活动"消息归因于WooCommerce产品附加的分类法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!