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

大小(Pa_Size)(单选或复选框)
--S(2)
-M(1)

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

wp_query products:

       $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过滤器不起作用.我知道如何进一步按属性进行筛选,但我不知道如何从这些产品中获取属性列表.

推荐答案

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

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文件中.测试和工作.

USAGE:,然后可以在代码中使用它,如下所示:

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

你会得到这样的东西:

enter image description here

Php相关问答推荐

在保留唯一键值对的情况下高效合并嵌套数组

Mysqli_stmt::Execute():结果字段元数据中存在过早的EOF

RediSearch PHP希望我在每次使用时定义所有字段

使用正则表达式搜索两个子字符串(没有重叠/共享字符)

如何将WooCommerce产品属性术语名称显示为链接术语?

在WooCommerce获取所有具有全球产品属性品牌的简单产品

针对给定产品类别的WooCommerce计数审核

shell_exec运行大型数据处理

WordPress:今天的计数';修改和发布的帖子

向WooCommerce管理订单总额添加自定义总额行

表单提交返回空白页

Mockery在模拟Redis连接时抛出错误:Mockery\Exception\BadMethodCallException

用ajax获取文件 - CORS问题&;设置缓存?

根据页面以及是否在促销,向 WooCommerce 中显示的价格添加前缀

Symfony:从控制器内部调用自定义命令

订单完成后创建 WooCommerce 促销代码

Laravel 添加到经过身份验证的用户的数据将应用于每个查询

URL中包含"&"与DirectoryIterator不兼容

将 HTML 表单链接到 SQLite 数据库

如何通过路由传递变量