Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing values of Product Array in Woocommerce

Unfortunately I'm not as strong as I'd like to be in pHp, so I might be asking a pretty basic question.

I'm just trying to figure out why I can't print the individual values of the product array.

print_r($product) returns:

WC_Product_Simple Object ( 
    [id] => 72 
    [post] => WP_Post Object ( 
        [ID] => 72 
        [post_author] => 1 
        [post_date] => 2015-09-23 21:54:50 
        [post_date_gmt] => 2015-09-23 21:54:50 
        [post_content] => 
        [post_title] => Simple Product #1 
        [post_excerpt] => 
        [post_status] => publish 
        [comment_status] => open 
        [ping_status] => closed 
        [post_password] => 
        [post_name] => simple-product-1 
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2015-09-23 21:55:48 
        [post_modified_gmt] => 2015-09-23 21:55:48 
        [post_content_filtered] => 
        [post_parent] => 71 
        [guid] => http://brantbweb.com/?post_type=product&p=72 
        [menu_order] => 0 
        [post_type] => product 
        [post_mime_type] => 
        [comment_count] => 0 
        [filter] => raw 
        ), 
    [product_type] => simple 
    [shipping_class:protected] => 
    [shipping_class_id:protected] => 0 
    [downloadable] => no 
    [regular_price] => 10 
    [price] => 10 
    [tax_status] => taxable 
    [manage_stock] => no 
    [stock_status] => instock 
)

I tried

echo $product->post_parent;

But that doesn't work.

So then I tried

echo $product[0];

But that prints an error.

My feeling is that the first option is working, just in a way I'm not expecting, so hopefully I can get an explanation as to why it wasn't working, and what I should be doing instead.

Thanks!

like image 743
Brant Barton Avatar asked Oct 20 '22 01:10

Brant Barton


1 Answers

According to your structure, you have a object $product with one attribute $post that is another object, so you can try:

print_r($product->post->post_parent);

Hope it helps!

like image 77
James Avatar answered Oct 27 '22 20:10

James