猿问

在 WooCommerce 中获取附加到产品的所有“活动”属性分类法?

我想获得附加到产品的所有“活动”属性列表,


因此,如果该属性存在,但不附加到任何产品,则不会显示。


我可以像这样将所有属性显示为下拉列表:


$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>';

}

但是通过这种方式我得到了所有属性,甚至没有附加非活动属性。


如何在 WooCommerce 中获取附加到产品的所有活动产品属性分类法?


慕哥9229398
浏览 107回答 1
1回答

莫回无

要获取所有活动的产品属性分类法(至少附加到产品),您将需要一个自定义的简单 sql 查询,如下所示(嵌入在 php 函数中):function wc_get_active_attribute_taxonomies() {&nbsp; &nbsp; global $wpdb;&nbsp; &nbsp; return $wpdb->get_results( "&nbsp; &nbsp; &nbsp; &nbsp; SELECT DISTINCT&nbsp; wat.*, tt.taxonomy&nbsp; &nbsp; &nbsp; &nbsp; FROM {$wpdb->prefix}woocommerce_attribute_taxonomies wat&nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN {$wpdb->prefix}term_taxonomy tt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON tt.taxonomy = CONCAT('pa_', wat.attribute_name)&nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN {$wpdb->prefix}term_relationships tr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON tt.term_taxonomy_id = tr.term_taxonomy_id&nbsp; &nbsp; &nbsp; &nbsp; WHERE tt.count > 0&nbsp; &nbsp; " );}代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。用法 (基于您的代码):只需更换:$attributes = wc_get_attribute_taxonomies();经过:$attributes = wc_get_active_attribute_taxonomies();注意:此查询输出还包括“分类法”参数。
随时随地看视频慕课网APP
我要回答