• Hello,

    I had a look into /integration/woocommerce.php and noticed that you get the product with the following piece of code:

    $prodid        = get_the_ID();
    $product       = get_product( $prodid );

    Are there special reasons for not receiving the product this way:

    global $product;

    It will eliminate 2 functions calls. Does that make sense?

    If $post is not set or has been modified and not reset yet and is not containing the correct ID, the following will cause a fatal error:

    
    $product_price = $product->get_price();

    By the way, that is the reason why I am writing this post. Perhaps, you need to add an early return if $product is not received for some reason. What do you think?

    I would make a pull request on Github on this matter but the version there is outdated.

    • This topic was modified 7 years, 11 months ago by skoldin.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    It depends on where you see this code. Some code runs outside of the main loop this I need to query the product.

    Feel free to add your changes to this GitHub project, this is where you can always see the latest changes:
    https://github.com/duracelltomi/gtm4wp

    Thanks,
    Thomas Geiger

    Thread Starter skoldin

    (@skoldin)

    Hi Thomas,

    This code is from /integration/woocommerce.php

    get_the_ID() won’t work outside the main loop anyway so if you can get the product ID this way, I assume, the global $product variable will be available as well.

    Thanks for the Github link, I will prepare the pull request as soon as possible so that you can review the change I mean.

    Regards,
    Igor Skoldin

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi Igor,

    Thanks for your contribution, I really appreciate it!

    Thomas

    Thread Starter skoldin

    (@skoldin)

    Hi Thomas,

    I performed more tests and had to hold this off. The reason is that while I expected the global $product variable to be always available when the $post is available (accordingly to WooCommerce documentation, global $product is set when the_post function is called, that is, at the same time as the global $post variable, from which you take the product ID), but it appears that is not always the case (sometimes $product contains just the name of the product but not the product object) and I am not sure why.

    So your way although is not that straightforward but seems to be more robust. I will do some investigation why WooCommerce is not assigning product object to the $product global variable when I have time and update the topic if I find something interesting.

    Regards,
    Igor

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi Igor,

    Thanks for the update, I am curious what you can get out of this, I am always open to make the code more lightweight.

    Thomas

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Getting product data (WooCommerce integration)’ is closed to new replies.