如何修改 WooCommerce 中的商店页面主查询以显示列表中每个产品的变体?

hor*_*gaz 3 php wordpress woocommerce

我在 WooCommerce 上开一家鞋店。每双鞋都有不同的颜色和尺码。我添加了颜色和尺寸作为属性,然后添加了一些产品作为可变产品。

我想在商店页面中列出所有产品,包括颜色变化(不包括尺寸!)...示例:

  • 可变产品 A 有黄色和黑色,尺寸为 36 至 40
  • 可变产品 B 有酒红色和白色,尺寸从 36 到 40

然后,我的商店页面应该有四个项目:

  • 产品A 黄色
  • 产品A 黑色
  • 产品B 酒
  • 产品B 白色

第一步:我试图以商店页面主查询为目标来修改它,但我不能!我正在这样做:

function custom_pre_get_posts( $query ){
    // attempt #1
    if( ( 'product' == get_post_type() || is_post_type_archive('product') ) ):
        $query->set( 'posts_per_page', '1' );
        return;
    endif;

    // attempt #2
    if( is_shop() ):
        $query->set( 'posts_per_page', '1' );
        return;
    endif;
}
add_action('pre_get_posts', 'custom_pre_get_posts');
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?提前致谢!

第二步:我将尝试修改主查询以仅针对我之前提到的变体...我也不知道如何做到这一点...有任何提示吗?

谢谢!!

小智 5

为我工作

\n
// ALTERAR A QUERY PRINCIPAL\nfunction fwp_archive_per_page( $query ) {\n    if ( is_tax( 'product_cat' ) || is_post_type_archive('product') ) {\n        \n        $query->set( 'showposts', 16 );\n        $query->set( 'posts_per_page', 16 );\n        $query->set( 'meta_query', array(\n                                      'relation'    => 'AND',\n                                    array(\n                                        'key'   => 'condicoes_de_exibicao',\n                                        'value'     => array('N\xc3\xa3o exibir no site', 'N\xc3\xa3o exibir em lugar nenhum'),\n                                        'compare'   => 'NOT IN',\n                                      )\n                                    ) );\n\n    }\n}\nadd_filter( 'pre_get_posts', 'fwp_archive_per_page' );\n
Run Code Online (Sandbox Code Playgroud)\n