从产品 WP_Query 获取 WooCommerce 产品属性单选按钮列表

问题描述 投票:0回答:1

在自定义页面上,我使用 wp_query 输出类别中的产品。如何从这些产品中输出属性列表及其编号以进行进一步过滤?

尺寸(pa_size)(单选或复选框)
-S (2)
-M (1)

颜色 (pa_color)(单选或复选框)
-红色 (15)
-蓝色 (4)
...

wp_查询产品:

       $query = new WP_Query($args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page'        => -1,
        'tax_query'             => array(
            array(
                'taxonomy'      => 'product_cat',
                'terms'         => 41,
            ),
        ),
    ));
    ?>
   <div class="products">
    <?php
       if ($query->have_posts()) :
        while ($query->have_posts()) :
            $query->the_post();
    
            wc_get_template_part('content', 'product-list');
    
        endwhile;
        wp_reset_postdata();
    endif;
    
    ?>
    </div>

例如

get_terms("pa_size");
显示所有属性。自定义页面上的 WC 过滤器不起作用。我知道如何按属性进一步过滤,但我不知道如何从这些产品中获取属性列表。

php wordpress woocommerce product taxonomy-terms
1个回答
0
投票

您可以使用以下自定义实用程序函数从 WP_Query 获取产品属性并显示每个产品属性术语的单选按钮:

function get_product_attributes_from_query( $query ){
    global $wpdb;

    $post_ids = implode(',', array_column( (array) $query->posts, 'ID', false) );

    return $wpdb->get_results("
        SELECT t.term_id, t.name, t.slug, tt.taxonomy, tt.count
        FROM {$wpdb->prefix}term_relationships as tr
        JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
        JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
        WHERE tt.taxonomy LIKE 'pa_%' AND tr.object_id IN ( {$post_ids} )
        GROUP BY t.term_id  ORDER BY tt.taxonomy
    " ); 
}

function display_related_product_attributes_radio_buttons( $query ) {
    if ( $query->have_posts() ) {
        echo ' <div class="attributes-radios">';

        if ( $results = get_product_attributes_from_query( $query ) ) {
            $terms_by_taxonomy = array(); // Initializing

            // Loop through attributes
            foreach ( $results as $result ) {
                // Group by attribute taxonomy
                $terms_by_taxonomy[$result->taxonomy][$result->term_id] = $result;
            }

            // Loop through grouped attributes by taxonomy
            foreach ( $terms_by_taxonomy as $taxonomy => $terms ) {
                printf('<p class="form-row" id="%s_field"><label for="%s"><strong>%s:</strong></label>
                <span class="woocommerce-input-wrapper">', 
                $taxonomy, $taxonomy, wc_attribute_label($taxonomy) );

                // Loop through terms from the current taxonomy
                foreach ( $terms as $term_id => $term ) {
                    printf('<label for="%s_%s" class="radio">
                    <input type="radio" class="input-radio" value="%s" name="%s" id="%s_%s"> <span>%s&nbsp;(%s)</span></label>', $term->slug, $taxonomy, 
                    $taxonomy, $term->slug, $taxonomy, $term->slug, $term->name, $term->count );
                }
                echo '</span></p>';
            }
        }
        echo '</div>';
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。已测试并工作。

用法: 然后您可以在代码中使用它,例如:

$query = new WP_Query($args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'ignore_sticky_posts'   => 1,
    'posts_per_page'        => -1,
    'tax_query'             => array(
        array(
            'taxonomy'      => 'product_cat',
            'terms'         => 41,
        ),
    ),
)); 

// Here we insert the function for example
display_related_product_attributes_radio_buttons( $query );

?>
<div class="products">
<?php
   if ($query->have_posts()) :
    while ($query->have_posts()) :
        $query->the_post();

        wc_get_template_part('content', 'product-list');

    endwhile;
    wp_reset_postdata();
endif;

?>
</div>

你会得到类似的东西:

© www.soinside.com 2019 - 2024. All rights reserved.