php – listing all sku with variations in a specific page

I am new to PHP/WordPress world. I’ve been trying to enlist sku for about 4 days now, but I just couldn’t. What I want is to get the sku codes of all the products (with their variations) registered in the woocommerce database. and i want to show these codes on a wordpress page i want.

I would have accomplished this with the code below.

$args = array(
    'post_type' => 'product', 
    'posts_per_page' => -1

$wcProductsArray = get_posts($args);

if (count($wcProductsArray)) {
    foreach ($wcProductsArray as $productPost) {
        $productSKU = get_post_meta($productPost->ID, '_sku', true);
        $productTitle = get_the_title($productPost->ID);
        echo '<li>'.$productTitle.' - '.$productSKU.'</li>';

But here’s the problem: it only shows the main products and their codes. It does not show the variations and their SKUs. Can you please help me to solve this. I’m about to go crazy!

Leave a Comment