Get products with specific attribute and in category – woocommerce


Retrieve products with specific attribute and in category – woocommerce

I am trying to get product of a specific category and variation.

Attribute name ‘List Product Price’ has 12 terms as “List Product Price Eight, List Product Price Eleven, List Product Price Five, List Product Price Four, List Product Price Nine, List Product Price Seven, List Product Price Six, List Product Price Ten, List Product Price Three, List Product Price Two, List Product Twelve”

Categories as “Abutments,Additional Material,Boxes,Cover Screws,Healing Caps,Implants,Impression Posts,Model Analogues,Software”.

I have tried 2 ways but cannot get result as desire

                                    `<?php     $args= array(

‘post_type’ => array(‘product’, ‘product_variation’),
‘post_status’ => ‘publish’,
‘posts_per_page’ => -1,
‘tax_query’ => array(
‘relation’ => ‘AND’,
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => $catterm
‘taxonomy’ => ‘pa_list-product-price’,
‘field’ => ‘slug’,
‘terms’ => $varterm

$nwproductssubset = new WP_Query($args);
if($nwproductssubset->have_posts()) {
while($nwproductssubset->have_posts()) {

   $product_id = $post->ID;
   $product = wc_get_product( $product_id );
   $discountpert = get_field('discount_percentage', $product_id );
   $prdctprice= $product->get_price();
   $reqularprice= $prdctprice * (100/$discountpert);
   $discountprice= (float)$prdctprice;

                                    <tr class="<?php echo "setblock".$setcbt; ?> " data-product-prdtid="<?php echo $product_id; ?>"  data-product-catt="<?php echo $catterm; ?>" data-product-attr="<?php echo  $varterm; ?>">
                                    <td><?php echo $product->get_sku(); ?></td>
                                        <td><?php echo $product->get_name(); ?></td>
                                        <td><?php echo sprintf("%.2f", $reqularprice); ?></td>
                                        <td><?php echo $product->get_price(); ?></td>
                                        <td class="flx quantity"><div class="qntymsg error-msg"></div> <span class="plus plus-seventytwo-inner"><i class="fa fa-plus" aria-hidden="true"></i></span> <span class="input-tag"><input type="text" data-maincat="<?php echo str_replace(" ","-",strtolower($catterm)); ?>" data-productid="<?php  echo $product_id; ?>" data-perqntyseventytwoprice="<?php echo $product->get_price(); ?>" data-perseventytwoqnty="0" data-qntyseventytwoprice="0" class="innrevntytwo qty qty-seventytwo" data-prdctmainsubtable="<?php echo $setcbt; ?>" min="0" value="0"></span> <span class="minus minus-seventytwo-inner minus-btn"><i class="fa fa-minus" aria-hidden="true"></i></span> </td>
                                        <td class="catsum"><?php echo $product->get_price(); ?></td>
                                        <td><?php echo $discountpert."%"; ?></td>

} // endwhile
wp_reset_postdata(); // VERY VERY IMPORTANT


Pradeep 4 years 2019-11-02T04:01:56-05:00 0 Answers 78 views 0

Leave an answer