• Resolved bykingpin

    (@bykingpin)


    I am new in Php/Wordpress world. I’ve been trying to list the sku for about 4 days, but I couldn’t do it. 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 in part 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 is the problem: It only shows the main products and their codes. It does not show the variations and their SKUs. Could you please help me solve this. I’m about to go crazy!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi
    try like this way:

    $statuses = array('publish', 'draft');
    
    // Args on the main query for WC_Product_Query
    $args = [
        'status'    => $statuses,
        'orderby'   => 'name',
        'order'     => 'ASC',
        'limit'     => -1,
    ];
    
    $vendor_products = wc_get_products($args);
    
    $list_array = array();
    
    foreach ($vendor_products as $key => $product) {
    
        if ($product->get_type() == "variable") {
    
            // Args on product variations query for a variable product using a WP_Query
            $args2 = array( 
                'post_parent' => $product->get_id(), 
                'post_type'   => 'product_variation', 
                'orderby'     => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ), 
                'fields'      => 'ids', 
                'post_status' => $statuses, 
                'numberposts' => -1, 
            ); 
    
            foreach ( get_posts( $args2 ) as $child_id ) {
                // get an instance of the WC_Variation_product Object
                $variation = wc_get_product( $child_id ); 
    
                if ( ! $variation || ! $variation->exists() ) {
                    continue;
                }
    
                $list_array[] = array(
                    'SKU'      => $variation->get_sku(),
                    'Name'     => $product->get_name() . " - " . $child_id,
                );
            }
    
        } else {
    
            $list_array[] = array(
                'SKU'      => $product->get_sku(),
                'Name'     => $product->get_name(),
            );
    
        }
    
    }
    
    return $list_array

    This time you will get all the product variations of your variable products.

    Thanks
    Ahir

    • This reply was modified 2 years, 4 months ago by Ahir Hemant.
    Thread Starter bykingpin

    (@bykingpin)

    hello hemant;
    thank you for your help but what do i need to add to show this data on the page? I use Php Code plugin for this job. I paste your code and click save button system show popup: There was an error

    • This reply was modified 2 years, 4 months ago by bykingpin.

    Thanks for pitching in @hemant-ahir!

    Hi there @bykingpin ??

    thank you for your help but what do i need to add to show this data on the page? I use Php Code plugin for this job. I paste your code and click save button system show popup: There was an error

    Thanks for reaching out! Have you tried adding it to a custom template? Feel free to learn more about this here.

    Kindly keep in mind we are not developers and only offer support for existing functionality in WooCommerce.

    Please see our Support Policy: https://www.woocommerce.com/support-policy/

    For assistance with customization or development with your site, we recommend that you seek help from:

    * A local web developer
    * Codeable.io
    * WooExperts
    * Stackexchange

    If you are comfortable coding yourself and have questions, I would also recommend that you consider:

    * WooCommerce developer Portal
    * WooCommerce Slack Community
    * Advanced WooCommerce Facebook group

    I hope that helps you to figure it out.

    Feel free to get back to us if you have further questions.

    • This reply was modified 2 years, 4 months ago by anastas10s. Reason: typos
    Thread Starter bykingpin

    (@bykingpin)

    Hi @anastas10s;

    this code didn’t work so i will try something else with c#.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘listing sku’ is closed to new replies.