【问题标题】:How to get only simple products and product variations (variable products excluded) based on a specific category in WooCommerce?如何根据 WooCommerce 中的特定类别仅获取简单的产品和产品变体(不包括可变产品)?
【发布时间】:2022-01-30 01:09:39
【问题描述】:

我需要检索以下所有简单产品和产品变体:

  • post_status 等于publish
  • 简单的产品和产品变体属于特定的产品类别(在我的例子中是移动);

不包括可变产品

到目前为止,我已经尝试过这段代码,但它不起作用:

$arg_product_filter = array(
    'nopaging'       => true,
    'post_status'    => array( 'publish' ),
    'posts_per_page' => -1,
    'post_type'      => array( 'product', 'product_variation' ),
    'tax_query'      => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_type',
            'field'    => 'slug',
            'terms'    => array( 'simple', 'variation' ),
        ),
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => 'mobile',
        )
    ),
);
$the_query = new WP_Query( $arg_product_filter );

此查询仅返回简单产品,如果我将关系更改为“或”,它也会返回可变产品。我做错了什么?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您可以使用以下自定义函数来获取简单产品和产品变体的 ID不包括可变产品

    此外,该函数仅通过根据指定为参数的产品类别过滤具有发布状态的产品返回。

    该函数将返回一个包含产品 ID 的数组。

    /**
     * Gets the ids of simple products and product variations with the following criteria:
     * - Only if the product is published;
     * - Excludes variable products;
     * - Products belong to a specific product category.
     * 
     * @param mixed  $term     Term to search for.
     * @param string $taxonomy Taxanomy in which to search for the term.
     * @param string $type     Type of term to search for. It can contain the following values: 'term_id', 'name' or 'slug' 
     * 
     * @return array Product IDs.
     */
    function get_simple_products_and_product_variations_ids_by_cat( $term, $type = 'slug', $taxonomy = 'product_cat' ) {
        global $wpdb;
        $sql = "SELECT ID
        FROM {$wpdb->prefix}posts
        INNER JOIN {$wpdb->prefix}term_relationships as tr ON ( ID = tr.object_id OR post_parent = tr.object_id )
        INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
        INNER JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
        WHERE post_type IN ( 'product', 'product_variation' )
        AND post_status = 'publish'
        AND ID NOT IN (
            SELECT DISTINCT post_parent AS parent_id
            FROM {$wpdb->prefix}posts
            WHERE post_type = 'product_variation'
            AND post_status = 'publish'
        )
        AND tt.taxonomy = '$taxonomy'
        AND t.$type = '$term'";
        $results = $wpdb->get_col( $sql );
    
        return array_map( 'intval', $results );
    
    }
    

    代码已经过测试并且可以运行。将其添加到您的活动主题的 functions.php 中。


    用法

    1. 根据产品类别ID(例如30)过滤产品:
    get_simple_products_and_product_variations_ids_by_cat( 30, 'term_id' );
    
    1. 产品类别名称(例如Red wines)过滤产品:
    get_simple_products_and_product_variations_ids_by_cat( 'Red wines', 'name' );
    
    1. 根据产品类别标签(例如red-wines)过滤产品:
    get_simple_products_and_product_variations_ids_by_cat( 'red-wines', 'slug' );
    

    回答

    您只获得简单产品的原因是产品类别和产品变体之间没有关系。唯一的关系是与可变产品(产品变体的post_parent)。

    你可以通过上面自定义函数的结果设置WP_Query类的post__in参数来获取产品,那么:

    $args = array(
        'nopaging'       => true,
        'posts_per_page' => -1,
        'post_type'      => array( 'product', 'product_variation' ),
        'post__in'       => get_simple_products_and_product_variations_ids_by_cat( 'fruit' ),
    );
    $loop = new WP_Query( $args );
    
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) {
            $loop->the_post();
            global $product;
    
            // do stuff
    
        }
    }
    

    相关答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 2018-08-26
      • 2019-01-23
      • 2018-12-19
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多