WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
[resolved] how to retrieve product details for wp_head? (14 posts)

  1. alekv
    Member
    Posted 9 months ago #

    Hi

    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?

    Thanks
    alekv

    http://wordpress.org/extend/plugins/woocommerce/

  2. alekv
    Member
    Posted 9 months ago #

    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);

  3. Roy
    Member
    Posted 9 months ago #

    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.

  4. alekv
    Member
    Posted 9 months ago #

    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 );
    $order->get_total();

    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.

  5. Roy
    Member
    Posted 9 months ago #

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

  6. alekv
    Member
    Posted 9 months ago #

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

    (The documentaion about the woocommerce global didn't help me...)

  7. Roy
    Member
    Posted 9 months ago #

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

  8. alekv
    Member
    Posted 9 months ago #

    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. http://core.trac.wordpress.org/ticket/3670

  9. Roy
    Member
    Posted 9 months ago #

    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.

  10. alekv
    Member
    Posted 9 months ago #

    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.

  11. Roy
    Member
    Posted 9 months ago #

    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?

  12. alekv
    Member
    Posted 9 months ago #

    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...

  13. Roy
    Member
    Posted 9 months ago #

    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.

  14. alekv
    Member
    Posted 9 months ago #

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

Reply

You must log in to post.

About this Plugin

About this Topic