• Resolved Harm10

    (@harm10)


    I have a site that sets meta data like product:weight in the page header.
    For that I use action wp_head and the content of global var $product.
    When on a product page this works fine.
    That site uses WP 6.6.2 and WC 9.4.3

    Now I am testing the new version of WP (6.7.1) together with WC 9.5.1 to find that the global var does exist on wp_head action but is not (yet) populated.
    Is this a bug?
    If not how can I set the desired meta tags in another way?

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter Harm10

    (@harm10)

    Can I please get a respons?

    This is messing up my meta data in the product page header.

    Plugin Support Mahfuzur Rahman(woo-hc)

    (@mahfuzurwp)

    Hi @harm10,

    Thank you for reaching out.

    The issue likely stems from the wp_head hook executing too early in the page load process. At that point, the $product object might not be fully initialized, leading to errors when accessing its properties.

    To reliably access product data, use the woocommerce_after_single_product_summary hook instead. This hook fires after the product summary is displayed, ensuring the $product object is fully populated. Here is an example:

    add_action( 'woocommerce_after_single_product_summary', 'add_product_weight_meta' );

    function add_product_weight_meta() {
    global $product;

    if ( is_product() ) {
    echo '<meta itemprop="weight" content="' . esc_attr( $product->get_weight() ) . '" />';
    }
    }

    I hope this helps, let us know if you have any more questions.

    Thread Starter Harm10

    (@harm10)

    Thanks for your info. But if I want to add meta tags to the header then this action will be fired in time?

    Plugin Support Moses M. (woo-hc)

    (@mosesmedh)

    Hi @harm10,

    The method explained above indeed fires after the product content is displayed on the page, while it ensures the $product object is fully populated, it may not be suitable for adding meta tags to the <head> section if that is what you’re referring to as header, as it executes too late in the rendering process.

    By the time this hook runs, the <head> section is already rendered, so the meta tags will not appear where they need to be.

    An alternative to this is to use a combination of wp and wp_head. Below is an example:

    add_action( 'wp', 'populate_product_meta_for_header' );

    function populate_product_meta_for_header() {
    // Ensure we are on a single product page
    if ( is_product() ) {
    // Add the meta tag to the wp_head hook
    add_action( 'wp_head', 'add_product_weight_meta' );
    }
    }

    function add_product_weight_meta() {
    global $product;

    // Double-check if $product is available
    if ( ! empty( $product ) ) {
    echo '<meta itemprop="weight" content="' . esc_attr( $product->get_weight() ) . '" />';
    }
    }

    You can try both methods to see which one works for you. Please note that providing code support beyond this is outside support scope of the forum. If you’re still having this issue, it’ll be better to seek professional help from codeable.io or use https://jobs.wordpress.net/

    Thread Starter Harm10

    (@harm10)

    Again thanks for your suggestion.
    I would like to point out that either WP or WC did change something in the sequence of events as before WP 6.7.1 and WC 9.5.1 $product was populated when action wp_head was fired!
    Can someone please respond on that?

    I tried the first suggestion with woocommerce_after_single_product_summary but that fires too late.
    The second one merely changes the sequence slightly but does not take care of populating $product in time?

    Currently I have found a solution by getting the product info myself via
    $product = wc_get_product( get_the_ID() );

    Maybe it would be a good addition to WC to offer some functionality in WC for adding meta tags? I am a hobby developer but surely other users of WC would like something like this too?

    Plugin Support Moses M. (woo-hc)

    (@mosesmedh)

    Hi @harm10,

    I’m glad you were able to find a solution by getting the product info yourself using wc_get_product(get_the_ID());.

    About wp_head working before WP 6.7.1 and WC 9.5.1 for populating $product, there are various ways on how to go about it and an update can alter different approach. Below is another approach which still makes use of wp_head with a combination of wc_get_product by ID.

    // gets the product data in the header
    add_action( 'wp_head', 'get_product_data' );
    function get_product_data() {
    // only on the product page
    if ( ! is_product() ) {
    return;
    }
    // gets the product object
    $product = wc_get_product( get_the_ID() );
    ?>
    HTML CODE HERE
    <?php

    If you feel this is a bug, you can report it on github.

    Thread Starter Harm10

    (@harm10)

    Thank you for your reply.
    The point I want to get across is: how should hobby web designers/developers like me be aware that either the sequence of actions or the population of global variables is changed for version to version?
    Not only for WP but also WC?

    I did not get a respons on my suggestion for adding meta tag functionality to WC? Will this be put on some enhancement list?

    Plugin Support Mahfuzur Rahman(woo-hc)

    (@mahfuzurwp)

    Hi @harm10,

    Thanks for your follow-up!

    When it comes to WordPress and WooCommerce updates, changes in the sequence of actions or the population of global variables can sometimes happen, especially with new versions. To stay informed about such changes:

    1. Review Release Notes: Always check the WordPress release notes and WooCommerce changelogs for updates on breaking changes or updates to global variables and hooks.

    2. Testing in a Staging Environment: Before updating to the latest versions, testing your site in a staging environment helps catch any issues caused by changes in global variables or actions.

    Regarding your suggestion for adding meta tag functionality to WooCommerce, I recommend submitting it to the WooCommerce GitHub repository or the WooCommerce Ideas board on their official site. While I don’t have visibility into the roadmap, submitting enhancements like this ensures they are considered by the development team.

    Thank you.

    Thread Starter Harm10

    (@harm10)

    Thanks for the additional information.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.