Support » Plugin: WooCommerce » how to retrieve product details for wp_head?

  • Resolved alekv



    I am writing a function which inserts some custom code into the header by adding an action to wp_head in the themes functions.php file.

    It works fine when viewing a cart. $woocommerce->cart->cart_contents_total; works as expected and returns the carts total value.

    But when viewing a product page I need to fetch the product ID and the product price. For instance $woocommerce->product->price; just gets me an empty value. Same for $woocommerce->product->id;.

    Is there any way I can access those values?

    Also I need to get the total value of a purchase from the thank you page, and I am running into the same problem. Any ideas?


Viewing 13 replies - 1 through 13 (of 13 total)
  • alright. I have found a solution to get the product ID.

    $product_id = get_the_ID();

    simple, isn’t it.

    and to get the product price use this:

    $price = get_post_meta( get_the_ID(), ‘_price’, true);

    Roy Ho


    An even better way is to use the product object.

    $product = get_product( get_the_ID() );
    echo $product->get_price();

    That creates the product object and you can now use any of the object’s methods to display anything about that product you want.

    excellent. Thanks splashingpixels !

    Would you also have a solution to get the order_total_value when adding a function to wp_head in the templates function.php file?

    I am searching since days. Something like

    $order = get_order( orderID );

    maybe? The only problem is, that I have to get the orderID from somewhere, but it seems not to be stored in a global variable….it only works when I am on a WooCommerce template, which is of no use in my case.

    Roy Ho


    Have you tried looking into the woocommerce global to see if you can use anything there?

    Sorry, I’m still a beginner. How can I look into the woocommerce global?

    (The documentaion about the woocommerce global didn’t help me…)

    Roy Ho


    Why do you need to put this value within the wp_head?

    That’s the only place i’ve found so far where wordpress doesn’t mess up my code. It contains a CDATA closing tag wich gets filtered by wordpress each time it is being sent to the browser.

    Roy Ho


    Because I don’t know exactly what you’re trying to do and why, my help is limited. But using wp_head doesn’t seem like the right place in my opinion unless you have a good reason for this. So perhaps you should explain in detail what you’re really trying to accomplish.

    I want to insert a google adwords tracking code with some custom variables which is why i can’t use one of the existing plugins. Besides, the header is the place where the tracking code should be placed anyway.

    Roy Ho


    Ok so I am assuming ONLY on the thankyou page is where you want to track the order info? So in your code you’re probably using is_page function to check if it is on thank you page?

    Yes. That’s basically what i do. I tried to add the function to woocommerce_thankyou but as mentioned earlier, the cdata tag gets messed up…

    Roy Ho


    Ok so in your function that you’re adding to the wp_head hook, you can try dumping the variables from the woocommerce global to see what info is in there when it reaches the thankyou page.

    global $woocommerce;
    var_dump( $woocommerce );

    Based on the info from that global see if anything is of use.

    nothing in the var_dump of help. I’ll try a few more approaches, like developing a pluging, trying to share a global variable…

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘how to retrieve product details for wp_head?’ is closed to new replies.