Woocommerce。通过子级的元数据查找父级产品

时间:2018-11-29 13:12:16

标签: wordpress woocommerce

有分组的产品。游览/游览。每个父产品都有孩子。儿童产品具有自定义元-wt_startwt_expired(游览开始和结束的日期)

我正在尝试修改搜索结果。因此,当转到链接site.com/?s=&post_type=product&wt_from=2018-11-27&wt_to=2019-07-24时,我需要显示与这些日期一致的游览。

有一个几乎可以正常工作的代码:

function me_search_query( $query ) {
    if ( $query->is_search ) {
        $meta_query = array();
        $from = strtotime( get_query_var('wt_from') );
        $to = strtotime( get_query_var('wt_to') );
        // add meta_query elements
        if( !empty( $from ) ){
            $meta_query[] = [
                    'key' => 'wt_start',
                    'value' => [
                        $from,
                        $to
                    ],
                    'compare' => 'BETWEEN'
            ];
        }

        if( !empty( $to ) ){
            $meta_query[] = [
                'key' => 'wt_expired',
                'value' => [
                    $from,
                    $to
                ],
                'compare' => 'BETWEEN'
            ];
        }

        if( count( $meta_query ) > 1 ){
            $meta_query['relation'] = 'OR';
        }

        if( count( $meta_query ) > 0 ){
            $query->set( 'meta_query', $meta_query );
        }
    };
}
add_filter( 'pre_get_posts', 'me_search_query');

但是在搜索结果中,我只需要显示父产品。 下面的代码有助于隐藏子产品并仅显示父产品,但父产品没有这些元wt_startwt_expired。因此,这对我的情况没有帮助。

add_action( 'woocommerce_product_query', 'only_grouped_products_query' );
function only_grouped_products_query( $q ) {
    print_r($q);
    //get current loop query
    $taxonomy_query = $q->get('tax_query') ;

    //appends the grouped products condition
    $taxonomy_query['relation'] = 'AND';
    $taxonomy_query[] = array(
        'taxonomy' => 'product_type',
        'field' => 'slug',
        'terms' => 'grouped'
    );


    $q->set( 'tax_query', $taxonomy_query );
    print_r($q->get('tax_query'));
}

0 个答案:

没有答案
相关问题