structured data attributes

#73294
  • Resolved Thibaut Fontaine
    Rank Math pro

    Hi,

    I’m using only variable products in my shop.

    By default, structured data sent by rankmath uses the ” Aggegate offers ” attribute.

    But google does not recommend this attribute for variable products as explained in this article: https://support.google.com/merchants/answer/6386198?hl=en in the products variants paragraph.

    How can I tell google that there are multiple prices on the page?

    Thank you in advance !

Viewing 15 replies - 1 through 15 (of 19 total)
  • Just to update, I’m using this code to set up correctly GTIN :

    add_filter( ‘rank_math/snippet/rich_snippet_product_entity’, function( $entity ) {
    if ( ! is_product() ) {
    return $entity;
    }

    $product = wc_get_product( get_the_ID() );
    if ( ! $product->is_type( ‘variable’ ) ) {
    $entity[‘gtin13’] = get_post_meta( get_the_ID(), ‘hwp_var_gtin’, true );
    return $entity;
    }
    $variations = $product->get_available_variations();

    if ( ! empty( $variations ) ) {
    $entity[‘gtin13’] = get_post_meta( $variations[0][‘variation_id’], ‘hwp_var_gtin’, true );
    }

    return $entity;
    } );

    I have found this code :
    dd_filter( ‘rank_math/snippet/rich_snippet_product_entity’, function( $entity ) {
    if ( ! is_product() ) {
    return $entity;
    }

    $product = wc_get_product( get_the_ID() );
    if ( ! $product->is_type( ‘variable’ ) ) {
    $entity[‘gtin13’] = get_post_meta( get_the_ID(), ‘_wpm_gtin_code’, true );
    return $entity;
    }

    $variations = $product->get_available_variations();
    if ( ! empty( $variations ) ) {
    $entity[‘offers’] = [];
    foreach ( $variations as $variation ) {
    $price_valid_until = get_post_meta( $variation[‘variation_id’], ‘_sale_price_dates_to’, true );
    $entity[‘offers’][] = [
    ‘@type’ => ‘Offer’,
    ‘description’ => strip_tags( $variation[‘variation_description’] ),
    ‘price’ => $variation[‘display_price’],
    ‘priceCurrency’ => get_woocommerce_currency(),
    ‘availability’ => $variation[‘is_in_stock’] ? ‘https://schema.org/InStock’ : ‘https://schema.org/OutOfStock’,
    ‘itemCondition’ => ‘NewCondition’,
    ‘priceValidUntil’ => $price_valid_until ? date_i18n( ‘Y-m-d’, $price_valid_until ) : ”,
    ‘url’ => $product->get_permalink(),
    ‘gtin13’ => get_post_meta($variation[‘variation_id’],’_wpm_gtin_code’, true ),
    ];
    }
    }

    return $entity;
    } );

    Can you give me the right code to set up correctly please ?

    Thanks in advance

    // GTIN données structurées

    add_filter( ‘rank_math/snippet/rich_snippet_product_entity’, function( $entity ) {
    if ( ! is_product() ) {
    return $entity;
    }

    $product = wc_get_product( get_the_ID() );
    if ( ! $product->is_type( ‘variable’ ) ) {
    $entity[‘gtin13’] = get_post_meta( get_the_ID(), ‘hwp_var_gtin’, true );
    return $entity;
    }
    $variations = $product->get_available_variations();

    if ( ! empty( $variations ) ) {
    $entity[‘gtin13’] = get_post_meta( $variations[0][‘variation_id’], ‘hwp_var_gtin’, true );
    }

    return $entity;
    } );

    add_filter( ‘rank_math/snippet/rich_snippet_product_entity’, function( $entity ) {
    if ( ! is_product() ) {
    return $entity;
    }

    $product = wc_get_product( get_the_ID() );
    if ( ! $product->is_type( ‘variable’ ) ) {
    $entity[‘gtin13’] = get_post_meta( get_the_ID(), ‘_wpm_gtin_code’, true );
    return $entity;
    }

    $variations = $product->get_available_variations();
    if ( ! empty( $variations ) ) {
    $entity[‘offers’] = [];
    foreach ( $variations as $variation ) {
    $price_valid_until = get_post_meta( $variation[‘variation_id’], ‘_sale_price_dates_to’, true );
    $entity[‘offers’][] = [
    ‘@type’ => ‘Offer’,
    ‘description’ => strip_tags( $variation[‘variation_description’] ),
    ‘price’ => $variation[‘display_price’],
    ‘priceCurrency’ => get_woocommerce_currency(),
    ‘availability’ => $variation[‘is_in_stock’] ? ‘https://schema.org/InStock’ : ‘https://schema.org/OutOfStock’,
    ‘itemCondition’ => ‘NewCondition’,
    ‘priceValidUntil’ => $price_valid_until ? date_i18n( ‘Y-m-d’, $price_valid_until ) : ”,
    ‘url’ => $product->get_permalink(),
    ‘gtin13’ => get_post_meta($variation[‘variation_id’],’_wpm_gtin_code’, true ),
    ];
    }
    }

    return $entity;
    } );

    Hello,

    Thank you for getting in touch with us.

    The support answer URL you shared only lists the required fields for the product. Th aggregate rating and review fields are optional but recommended properties. Please take a look here for reference: https://developers.google.com/search/docs/data-types/product

    The only warnings I see on your products are regarding Aggregate rating and review. Please note that Rank Math takes the schema data from your product pages automatically and turns it into Schema-ready content. Likewise, the aggregateRating and review fields are taken from your actual product reviews that your buyers/customers leave on your products.

    If you don’t have any reviews yet, that is going to show up as a warning in the schema testing tool. That’s because Google will actually look for those fields.

    Please acquire some reviews and ratings to get rid of the aggregateRating and review warnings.

    The following is the code that provided to resolve the gtin issue for variable products on your site:

    
    add_filter( ‘rank_math/snippet/rich_snippet_product_entity’, function( $entity ) {
    if ( ! is_product() ) {
    return $entity;
    }
    
    $product = wc_get_product( get_the_ID() );
    if ( ! $product->is_type( ‘variable’ ) ) {
    $entity[‘gtin13’] = get_post_meta( get_the_ID(), ‘hwp_var_gtin’, true );
    return $entity;
    }
    $variations = $product->get_available_variations();
    
    if ( ! empty( $variations ) ) {
    $entity[‘gtin13’] = get_post_meta( $variations[0][‘variation_id’], ‘hwp_var_gtin’, true );
    }
    
    return $entity;
    } );
    

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    I have find the first code in one ticket in rankmath.

    I have the same issue of this ticket : https://support.rankmath.com/ticket/gtin-numbers-for-variable-products-2/

    Sorry if I was not clear.

    Hello,

    The aggregate offer is an attribute of the offer property, you can check here to understand this: https://schema.org/Offer. It is usually collected from the product actual rating. There are rating plugins you can use to add the ratings & reviews yourself such as judgeme, which only requires a small code to integrate with Rank math schema.

    As for the code, I am not quite sure which correct code you require. Could you let us know exactly what code you need so we can help you with this.

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hello,

    To add sku in the Offer entity, please replace the filter code you added with the following:

    
    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
    	if ( ! is_product() ) {
    		return $entity;
    	}
    
    	$product = wc_get_product( get_the_ID() );
    	if ( ! $product->is_type( 'variable' ) ) {
    		$entity['gtin13'] = get_post_meta( get_the_ID(), '_wpm_gtin_code', true );
    		return $entity;
    	}
    
    	$variations = $product->get_available_variations();
    	if ( ! empty( $variations ) ) {
    		$entity['offers'] = [];
    		foreach ( $variations as $variation ) {
    			$price_valid_until = get_post_meta( $variation['variation_id'], '_sale_price_dates_to', true );
    			$entity['offers'][] = [
    				'@type'           => 'Offer',
    				'description'     => strip_tags( $variation['variation_description'] ),
    				'price'           => $variation['display_price'],
    				'priceCurrency'   => get_woocommerce_currency(),
    				'availability'    => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
    				'itemCondition'   => 'NewCondition',
    				'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '',
    				'url'             => $product->get_permalink(),
    				'gtin13'          => get_post_meta($variation['variation_id'],'_wpm_gtin_code', true ),
    				'sku'          => get_post_meta($variation['variation_id'],'_sku', true ),
    			];
    		}
    	}
    
    	return $entity;
    } );
    

    And for the GTIN, please check in which key the GTIN value is stored in your Database in postmeta table. On the other user’s site from where you took the code, the gtin value was stored in the _wpm_gtin_code meta. On your site, it might be stored in a different key. Please check and replace the _wpm_gtin_code with your gtin meta_key.

    That will fix the issue. Please let us know, if there is anything else.

    Thank you.

    Thanks for your reply ! we are moving in the right direction 🙂

    Just two things more :

    1. How can I add ”Price valid until ” for each offer ?
    2. How can I add GTIN and SKY for each offer ?

    https://prnt.sc/trp697

    Indeed, as you can see in the structured data only the SKU and GTIN of the default variation appear.

    I would like a SKU and a GTIN to appear for each of the offers and variations.

    Currently I’m using this code :

    // GTIN données structurées
    
    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
        if ( ! is_product() ) {
            return $entity;
        }
        
        $product = wc_get_product( get_the_ID() );
        if ( ! $product->is_type( 'variable' ) ) {
            $entity['gtin13'] = get_post_meta( get_the_ID(), 'hwp_var_gtin', true );
            return $entity;
        }
        $variations = $product->get_available_variations();
        
        if ( ! empty( $variations ) ) {
            $entity['gtin13'] = get_post_meta( $variations[0]['variation_id'], 'hwp_var_gtin', true );
        }
    
        return $entity;
    } );
    
    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
    	if ( ! is_product() ) {
    		return $entity;
    	}
    
    	$product = wc_get_product( get_the_ID() );
    	if ( ! $product->is_type( 'variable' ) ) {
    		$entity['gtin13'] = get_post_meta( get_the_ID(), 'hwp_var_gtin', true );
    		return $entity;
    	}
    
    	$variations = $product->get_available_variations();
    	if ( ! empty( $variations ) ) {
    		$entity['offers'] = [];
    		foreach ( $variations as $variation ) {
    			$price_valid_until = get_post_meta( $variation['variation_id'], '_sale_price_dates_to', true );
    			$entity['offers'][] = [
    				'@type'           => 'Offer',
    				'description'     => strip_tags( $variation['variation_description'] ),
    				'price'           => $variation['display_price'],
    				'priceCurrency'   => get_woocommerce_currency(),
    				'availability'    => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
    				'itemCondition'   => 'NewCondition',
    				'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '',
    				'url'             => $product->get_permalink(),
    				'gtin13'          => get_post_meta($variation['variation_id'],'hwp_var_gtin', true ),
    			];
    		}
    	}
    
    	return $entity;
    } );

    Hello,

    Thank you for the follow up.

    The gtin value seems to be showing according to the selected variation. For instance if you change the default variation and test the URL and test the URL schema again, you will note a different gtin value under offers. Here are two screenshots of the same product with different variations: https://i.rankmath.com/TutNfl, https://i.rankmath.com/vt4ShT

    For the priceValidUntil, please add a sale date validity to your product to fix this.

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hi Michael,

    Thanks for you reply.

    Two other questions :

    1. How can I add price valid until ? In woocommerce ?

    2. Ass we added GTIN, can we add SKU for each variation ?

    Thanks in advance

    Any updates ?

    Hello,

    To assign a constant date you can modify the price valid until value in the code provided above to have a specific date: 'priceValidUntil' => '2025-12-31';

    You can also modify the date variable to be a dynamic date depending in the product posted date. You can seek the help of a developer to change this and assign it to the priceValidUntil in the code provided above.

    If you have any further question(s), please let us know. Thank you.

    Hi Michael,

    Thanks for your reply !

    I’m added this following code :

    'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '2025-12-31',

    I’m sorry but as you can see in the test of structured data, I have no ”Price valid until”

    Maybe I make a mistake ?

    Thanks in advance

    Hello,

    Please remove the added code. The variable $price_valid_until in the code provided is meant to get the date from the value set as sale_price_dates_to in your WooCommerce product variation settings. Please add some date in the settings or assign the value manually as $price_valid_until = ‘Your_date’; which will set a constant date for all our products

    Hope that helps you.
    ​​​​​​

    Can you give me the right code of this line to add ‘2025-12-31’

    'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '',

    Thanks in advance

Viewing 15 replies - 1 through 15 (of 19 total)

You must be logged in to reply to this ticket.