Home > other >  Get current product category all products parent sku's and display them in array - Woocommerce
Get current product category all products parent sku's and display them in array - Woocommerce

Time:11-07

I'm trying to get all product sku's in a current category. I have this code so far which is getting me only the IDs. I have also tried to use meta_query but I get some errors. I have did some research over the portal and other sites but I did not get it at all. What do I need to do is to display all curent Woocommerce product category products parent sku's in all_ids array.

Code so far

$all_ids = get_posts( array(
      'post_type' => 'product',
      'numberposts' => -1,
      'post_status' => 'publish',
      'fields' => 'ids',
      'tax_query' => array(
        'relation' => 'AND',
         array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'tenisky',
            'operator' => 'IN',
         )
        ),
        ) );

Output which gets me the data into my HTML data layers.

<?php echo "'" . implode ( "', '", $all_ids ) . "'"; ?>

Thank you in advance!

CodePudding user response:

I think you should be able to solve this using get_post_meta:

$all_ids = get_posts( array(
      'post_type' => 'product',
      'numberposts' => -1,
      'post_status' => 'publish',
      'fields' => 'ids',
      'tax_query' => array(
        'relation' => 'AND',
         array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'tenisky',
            'operator' => 'IN',
         )
        ),
        ) );

$idsku = []; 

for ($index = 0; $index < count($all_ids); $index  ) {
    $idsku[$index] = get_post_meta($all_ids[$index], '_sku', true);
}
  • Related