Filter by specific product attributes values for custom product list in Woocommerce

I have a code that I want to show products in a tab for each term of an attribute (1 tab for each term).
The tabs are working and collecting the proper terms. The problem is that I’m having trouble showing only the products for that selected term in the foreach statement.
The products are shown in a modal that opens up in the cart. In this modal, it is shown a few produtcs that the customer can select.
The issue is that when I insert the commented code to try to filter the products (so each tab will only show the products that are associated with the term for that tab), it doesn’t work. Maybe I’m trying to filter it incorrectly
Here’s the code:

<div class=”tab”>
<?php
$terms = get_terms(“pa_salto-cor”);
foreach ( $terms as $term ) {

echo ‘<button class=”tablinks” onclick=”openTab(event, &apos;’ . $term->name . ‘&apos;)”>’ . $term->name . ‘</button>’;

}
?>
</div>
<?php

$terms = get_terms(“pa_salto-cor”);
foreach ( $terms as $term ) {
echo ‘<div id=”‘ . $term->name . ‘” class=”tabcontent”><br>’;
echo ‘<h3>’ . $term->name . ‘</h3>’;
echo ‘<form action=”‘ . admin_url( ‘admin-ajax.php’ ) . ‘” method=”post”>’;
echo ‘ <input type=”hidden” name=”action” value=”wfg_add_gifts”/>’;
wp_nonce_field( ‘wfg_add_free_gifts’, ‘_wfg_nonce’ );

if ( ! empty( $wfg_free_products ) ):
foreach ( $wfg_free_products as $product ):
if ( empty( $product->detail ) ) {
continue;
}
// I believe the code below is not working
$product = array(
‘post_type’ => ‘product’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘pa_salto-cor’,
‘field’ => ‘slug’,
‘terms’ => ‘preto’,
),
),
);

// End filter code

echo ‘ <div class=”wfg-gift-item”>’;
echo ‘ <div class=”wfg-heading”>’;
echo ‘ <input type=”checkbox” class=”wfg-checkbox” name=”wfg_free_items[]”‘;
echo ‘ id=”wfg-item-‘ . $product->detail->ID . ‘”‘;
echo ‘ value=”‘ . $product->detail->ID . ‘”/>’;
echo ‘ <label for=”wfg-item-‘ . $product->detail->ID . ‘” class=”wfg-title”>’;
echo ‘ <img src=”‘ . $product->image . ‘” style=”width:150px; height:150px;” alt=””/>’;
echo ‘ </label>’;
echo ‘ <h3>’ . $product->detail->post_title . ‘</h3>’;
echo ‘ </div>’;
echo ‘ </div>’;
endforeach;

echo ‘ <div class=”wfg-actions”>’;
echo ‘ <button class=”button wfg-button wfg-add-gifts”>’;
$add_gift_text = WFG_Settings_Helper::get( ‘popup_add_gift_text’, false, ‘global_options’ );
if ( false !== $add_gift_text ) {
echo $add_gift_text;
} else {
echo WFG_Common_Helper::translate( ‘Add Gifts’ );
}
echo ‘ </button>’;
echo ‘ <button class=”button wfg-button wfg-no-thanks” type=”button”>’;
$cancel_text = WFG_Settings_Helper::get( ‘popup_cancel_text’, false, ‘global_options’ );
if ( false !== $cancel_text ) {
echo $cancel_text;
} else {
echo WFG_Common_Helper::translate( ‘No Thanks’ );
}

echo ‘ </button>’;
echo ‘ </div>’;
endif;
echo ‘ </form>’;
echo ‘ </div>’;

}
?>

Please note that I used the term “preto” as an example, because I know it’s one of the possibilities. After it’s working, I’ll use $term->name so it gets the dynamic value for each product.
Any help would be greatly appreciated.
Thanks in advance!

Read more here:: Filter by specific product attributes values for custom product list in Woocommerce

Leave a Reply

Your email address will not be published. Required fields are marked *