首页 > 解决方案 > 在商店页面上显示可用属性

问题描述

我想在主页上显示可用的属性。为此,我有这段代码,它可以工作,但它显示了所有属性,而不仅仅是可用的属性。

function display_shop_loop_product_attributes() {
    global $product;
    // Define you product attribute taxonomies in the array
    $product_attribute_taxonomies = array( 'pa_size1', 'pa_size2', 'pa_faction', 'pa_gender' );
    $attr_output = array(); // Initializing
    // Loop through your defined product attribute taxonomies
    foreach( $product_attribute_taxonomies as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = wc_attribute_label( $taxonomy, $product );
            $term_names = $product->get_attribute( $taxonomy );
            if( ! empty($term_names) ){
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_nam.' '.$term_names.'</span>';
            }
        }
    }
    // Output
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
} ```

标签: attributes

解决方案


推荐阅读