Обработка видимости Woocommerce для скрытых продуктов в запросе WP

Можете ли вы помочь мне с этим? У меня есть список продуктов, и я хочу скрыть те продукты, которые отмечены как скрытые на детали видимости продукта. Вот код, который у меня есть:

 $args = array(
                'posts_per_page' => -1,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        // 'terms' => 'white-wines'
                        'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id,
                        'visibility' => 'visible' //NOT WORKING...
                    )
                ),
                'post_type' => 'product',
                'orderby'    => 'menu_order',      
                'order'           =>  'ASC',      

            );
            $products = new WP_Query( $args );

            if(isset($_GET['staging']) && $_GET['staging'] == "true") {
                echo "<pre>" . print_r($products) . "</pre>";
            }

Я хочу отобразить все продукты, отмеченные как видимые.


person Jerielle    schedule 30.10.2018    source источник


Ответы (3)


Начиная с Woocommerce 3, видимость продукта обрабатывается таксономией product_visibility для термина exclude-from-catalog, поэтому вам нужно использовать второй массив в массиве налоговых запросов:

$terms = array( $product_categories[$wc_arr_key[$wc_cat_id]]->term_id );

$products = new WP_Query( array(
    'post_type'         => 'product',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'tax_query'         => array(
        'relation'      => 'AND',
        array(
            'taxonomy'  => 'product_cat',
            'field'     => 'term_id',
            'terms'     => $terms
        ),
        array(
            'taxonomy'  => 'product_visibility',
            'terms'     => array('exclude-from-catalog'),
            'field'     => 'name',
            'operator'  => 'NOT IN',
        ),
    ),
    'orderby'           => 'menu_order',
    'order'             =>  'ASC',
) );


if(isset($_GET['staging']) && $_GET['staging'] == "true") {
    echo "<pre>" . print_r($products) . "</pre>";
}

Проверено и работает.

Связано: Изменения базы данных для продуктов в woocommerce 3

person LoicTheAztec    schedule 30.10.2018

Используйте приведенный ниже код, чтобы исключить hidden товаров и отображать только visible.

$args = array(
                'posts_per_page' => -1,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        // 'terms' => 'white-wines'
                        'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id
                    )
                ),
                'meta_query' => array(
                    array(
                       'key'       => '_visibility',
                       'value'     => 'hidden',
                       'compare'   => '!='
                         )
                 ),
                'post_type' => 'product',
                'orderby'    => 'menu_order',      
                'order'           =>  'ASC'      

            );
            $products = new WP_Query( $args );

            if(isset($_GET['staging']) && $_GET['staging'] == "true") {
                echo "<pre>" . print_r($products) . "</pre>";
            }
person raju_odi    schedule 30.10.2018

WooCommerce сохраняет эти данные как метаданные, поэтому вам нужно будет запустить мета-запрос для имени _visibility. Это будет выглядеть как

'meta_query' => array(
    array(
        'key'       => '_visibility',
        'value'     => 'hidden',
        'compare'   => '!=',
    )
)

Это приведет к удалению всех сообщений, у которых мета _visibility не равна скрытому

person WebguruInfosystems    schedule 30.10.2018