Display Woocommerce products based on including AND excluding Product attributes

Viewed 33

I want to show products on archive page which are having a specific Product Attribute (pa_1) but not having another Product Attribute (pa_2). Some products have pa_1 only, others pa_1 and pa_2. There are no products with only pa_2.

Product pa_1 pa_2
First X
Second X X
Third X

In the example above, I only want to display the First and the Third product.

Code I already have:

<?php

$orderby = 'count';
$order = 'desc';
$hide_empty = true ;
$cat_args = array(
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
    'exclude' => array()
);


$terms = get_terms("pa_1", $cat_args );
$terms2 = get_terms("pa_2", $cat_args );

if ($terms2 < 0) {

foreach ( $terms as $term ) { 

// Display products 

}}
else { 

// Don't show products

}

?>

I believe I am almost there, but need a little help to make this code work. Thanks.

0 Answers
Related