WooCommerce GTIN

#21510
  • Resolved Smartpack
    Rank Math free

    Hi I have added code to functions.php and added a GTIN to woocommerce inventory tab but the GTIN is not being picked up by Google Structured Data and it throws an error “this product is missing a global identifier” Are you able to help resolve this error without recommending that I install one of the “add GTIN” plugins as they are all outdated and untested with current versions of wordpress?

    • This topic was modified 5 years ago by Smartpack.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Michael Davis
    Rank Math pro

    Hello,

    Thank you for contacting Rank Math today.

    Since your gtin value is saved as postmeta, you can use the following filter to dynamically load the value from the database:

    /**
     * Filter to Short-circuit Schema if a 3rd party is interested in generating their own data.
     * Replace $schmea_type with schema name like article, review, etc.
     * @param array $entity Snippet Data
     * @return array
     */
    add_filter( "rank_math/snippet/rich_snippet_{$schema_type}", function( $entity ) {
     return $entity;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​

    Smartpack
    Rank Math free

    Thanks I added your code but there is still a missing GTIN or does it take some time to update itself?

    Michael Davis
    Rank Math pro

    Hello,

    Thank you for contacting Rank Math today.

    You need to add some custom code to this filter to get the saved value from the current product and pass it to the product schema. Here is an example that would work for single products:

    /**
     * Filter to add customize product schema.
     *
     * @param array $entity Snippet Data
     * @return array
     */
    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
        global $post;
        $gtin_value = get_post_meta($post->ID,'_gtin', true);
        if($gtin_value){
        $entity['gtin8'] = $gtin_value;
        return $entity;
        }
        return $entity;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​

    Smartpack
    Rank Math free

    Brilliant! Works Perfectly. Thankyou! Would it be possible to ask for code that passes the GTIN for variable products as well?

    Michael Davis
    Rank Math pro

    Hi there,

    Thanks for the follow up.

    Could you please try the following code:

    /**
     * Filter to add customize product schema.
     *
     * @param array $entity Snippet Data
     * @return array
     */
    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
        $product = new WC_Product( get_the_ID() );
        if( $product->is_type( 'simple' ) ){
             $gtin_value = get_post_meta($product->get_id(),'_gtin', true);
             $entity['gtin8'] = $gtin_value;
             return $entity;
        }elseif($product->is_type( 'variable' )){
             $variation = new WC_Product_Variation(get_the_ID());
             $gtin_value = get_post_meta($variation->get_id(),'_gtin', true);
             $entity['gtin8'] = $gtin_value;
             return $entity;
        }else{
             return $entity;
        }
        
    });

    Let me know how this goes.

    Thank you.

    Smartpack
    Rank Math free

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Smartpack
    Rank Math free

    Hi I am not sure the updated code is working? it adds the word “Array” to the SKU field and GTIN of the first product variant and adds the correct SKU but no GTIN to the second product variant?

    I have added the product URL and login to this ticket. Many thanks for your help.

    Michael Davis
    Rank Math pro

    Hi there,

    I cannot find the code that you are using to register and update the GTIN field. It looks like this code is the one that is updating the array value on the variable products as shown below:
    GTIN

    Smartpack
    Rank Math free

    The Code can be located from the left hand main wordpress Menu under Dynamik > Custom > Functions

    /**
    * Adding Custom GTIN Meta Field
    * Save meta data to DB
    */

    Thanks

    Smartpack
    Rank Math free

    Wordpress Menu under Dynamik > Custom > Functions

    /**
    * Adding Custom GTIN Meta Field
    * Save meta data to DB
    */

    Thanks

    Michael Davis
    Rank Math pro

    Hello,

    Thank you for contacting Rank Math today.

    Please regenerate a new access token, the current one has already expired.

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 11 replies - 1 through 11 (of 11 total)

The ticket ‘WooCommerce GTIN’ is closed to new replies.