How to get all product in the WooCommerce programa

2020-06-24 05:12发布

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?

4条回答
太酷不给撩
2楼-- · 2020-06-24 06:05

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; 
查看更多
3楼-- · 2020-06-24 06:05

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();
        ?>
查看更多
forever°为你锁心
4楼-- · 2020-06-24 06:07

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();
查看更多
Melony?
5楼-- · 2020-06-24 06:07

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.

查看更多
登录 后发表回答