WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently building a Theme for a site that is using WooCommerce to provide a shop to Customers. I have just started on it, and I am currently working on the Product Category pages. I have hit a road block trying to fetch the Product object.

I have read that using the variable global $product should return the WC_Project object, but when I do the_post(); var_dump($product), NULL is returned.

I tried to then create a Product object by doing the_post(); $product = new WC_Product(get_the_ID()), but when I did the var_dump($product) on that, it gave me general information about the product (post_name, post_description, etc), but nothing further than if I called get_post().

Can someone tell me what I have missed please?

share|improve this question
up vote 2 down vote accepted

Sorry, I have almost instantly found the solution. Thought about deleting the question, but in case others have the same problem, I will leave it here.

To get the Product object with all the required attributes, you need to call get_product() after the_post(), and that will return the Product object for you to use.

share|improve this answer
Please return to this answer and accept it :-) – Pieter Goosen Jun 12 '15 at 9:44
@PeterGoosen thanks for the reminder :-) – topherg Jun 12 '15 at 10:17
get_product() is deprecated wc_get_product() should replace it... Anyway it is just a shorthand for: WC()->product_factory->get_product( $the_product, $args ) – jave.web May 23 at 21:40

You could use a custom query to fetch the product object. Here is a snippet to get the stock count. Take a look at the product class: http://docs.woothemes.com/wc-apidocs/class-WC_Product.html for functions available for the product object.

<?php $args = array(
            'post_type'         => 'product',
            'post_status'       => 'publish',
            'posts_per_page'    => -1,
            'orderby'           => 'title',
            'order'             => 'ASC',               
            'tax_query' => array(
                    'taxonomy'  => 'product_type',
                    'field'     => 'slug',
                    'terms'     => array('simple'),
                    'operator'  => 'IN'

        $loop = new WP_Query( $args );
        $stock_count = array();
        while ( $loop->have_posts() ) : $loop->the_post();

                        global $product;
                        $stock_count[] = $product->get_stock_quantity();


        // count the array values and output them
        echo "<h2>Stock Count: ".array_sum($stock_count)."</h2>";
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.