3

I want to get all the product data in the WooCommerce (product sku, name, price, stock count, availability and etc.) Can I do that using wp-query?

helgatheviking
  • 25,596
  • 11
  • 95
  • 152
Kasun Abaywardana
  • 340
  • 2
  • 4
  • 17
  • In woocommerce plugin the all templete already exists find it and copy code to your custom templete ..they are custom post types with meta added u can also use custom post type query and get post meta method – Maninderpreet Singh Apr 03 '16 at 02:09
  • 2
    Just a friendly tip, you may want to read over this page: [How-To-Ask Guide](https://stackoverflow.com/help/how-to-ask) so you can always be sure that your questions are easily answerable and as clear as possible. Be sure to include any efforts you've made to fix the problem you're having, and what happened when you attempted those fixes. Also don't forget to your code and any error messages! – Matt C Apr 03 '16 at 04:18
  • 2
    All product data is post meta. But I agree with Matthew, it isn't clear what you're asking. – helgatheviking Apr 03 '16 at 09:11

5 Answers5

5

This way you can get all products via wp_query :

global $wpdb;

$all_product_data = $wpdb->get_results("SELECT ID,post_title,post_content,post_author,post_date_gmt FROM `" . $wpdb->prefix . "posts` where post_type='product' and post_status = 'publish'");

And if you want further product data then it will be like this :

$product = wc_get_product($product_id);
$product->product_type;
get_post_meta($prodyct_id, '_regular_price');
$product->get_available_variations();
Maha Dev
  • 3,915
  • 2
  • 31
  • 50
2

Thanks for all reply.I have resolve that like billows

$full_product_list = array();
$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1));

    while ($loop->have_posts()) : $loop->the_post();
        $theid = get_the_ID();
        $product = new WC_Product($theid);
        if (get_post_type() == 'product_variation') {

            // ****************** end error checking *****************
        } else {
            $sku = get_post_meta($theid, '_sku', true);
            $selling_price = get_post_meta($theid, '_sale_price', true);
            $regular_price = get_post_meta($theid, '_regular_price', true);
            $description=get_the_content();
            $thetitle = get_the_title();

        }
        // add product to array but don't add the parent of product variations
        if (!empty($sku))
            $full_product_list[] = array("PartyID" => (int) $party_id,"Description"=> $description,
                "ExternalNumber" => $theid, "ProductName" => $thetitle, "sku" => $sku,
                "RegularPrice" => $regular_price, "SellingPrice" => $selling_price,
                "ExternalProductCategoryId" => $cat_id, "ExternalProductCategoryName" => $cat_name);
    endwhile; 
Kasun Abaywardana
  • 340
  • 2
  • 4
  • 17
1

When your building out your query set the post type to be product

$query->set('post_type', 'product');

And that will only search through woocommerce products.

Also if you are creating a theme, you can take any file from the /wp-content/plugins/woocommerce/templates/ directory and put it inside of /wp-content/themes/<yourTheme>/woocommerce to override any woocommerce page.

hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
1

you can write this code in page that you want to display all product information

<?php
            $args = array(
                'post_type'      => 'product',
                'posts_per_page' => 10
            );

            $loop = new WP_Query( $args );

            while ( $loop->have_posts() ) : $loop->the_post();
            echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
                global $product;
                //echo $product;
                echo $product->get_id();
                echo $product->get_name();
                echo $product->get_sale_price();
                echo $product->get_regular_price();
                echo $product->get_sku();
                echo $product->get_low_stock_amount();
                echo $product->get_review_count();
                echo $product->get_short_description();
                $ratting = $product->get_rating_counts();
                for($i = 0 ; $i < count($ratting); $i++) {
                    echo $ratting[i];
                }
            endwhile;

            wp_reset_query();
        ?>
  • Welcome to StackOverflow. When answering a question, please also provide some context and explanation of what you have done so that people reading your answer can understand what is going on in your response. – Alan Jan 13 '19 at 21:37
1

This is an old question; the answers using WP_Query() are obsolete since 2018.

See my answer here.

Christian Lescuyer
  • 18,893
  • 5
  • 49
  • 45