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 WooCommerce Product object.
I have read that using the variable global $product
should return the WC_Product 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?
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.
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()
afterthe_post()
, and that will return the Product object for you to use.