URL Structured data

#75382
  • Resolved Thibaut Fontaine
    Rank Math pro

    Hello,

    I am using the following code to otpimize my structured data. I use offer since all of my products are variable products.

    // 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 ) : '2025-12-31',
    				'url'             => $product->get_permalink(),
    				'gtin13'          => get_post_meta($variation['variation_id'],'hwp_var_gtin', true ),
    			];
    		}
    	}
    
    	return $entity;
    } );

    As you can see by testing on this url: https://lerepaireduchef.fr/barbecue-kamado/

    I have one URL per offer and per variation. But this URL is always the same!

    I need to have a different URL per offer. Indeed, I use a different URL by variation as you can on my product page :
    https://lerepaireduchef.fr/barbecue-kamado/?attribute_pa_format=mini&attribute_pa_chariot=non&attribute_pa_options=non&attribute_pa_housse=non

    How can I add these URLs in the structured data please?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Alberto
    Rank Math business

    Hello,

    Thank you for contacting Rank Math today.

    We are usually unable to provide custom code since we are a small team but if you have any particular questions, we are here to help you.

    About your issue, I think you should modify the code with:

    $entity['offers'][] = [
    'sku'          => $variation['variation_id']->get_sku(),
    ];

    It is just a little advice but it should help you to modify your code.

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hello,
    I’m talking about URL you answer me about SKU.

    Did you understand my problem?

    Alberto
    Rank Math business

    Hello,

    I am sorry, I messed the two threads you posted and I posted the same answer on both of them.

    About your issue having the same URL, I recommended you to check this link: https://webmasters.stackexchange.com/questions/58256/schema-org-product-variations-with-same-url-problem

    Looking forward to help you.

    Hi Alberto,

    Thanks for your reply !

    But I need to add a different URL for each variation:)

    Do you know the right function to have URL for each variation ?

    I just have to customize this line :’url’ => $product->get_permalink(),

    Thanks in advance

    Hi,

    Maybe it’s more clear with picture 🙂

    As you can see here : https://prnt.sc/tv2k08

    It’s always the same URL, I just would like to have my customize URL in each offer.

    It’s my last question for structured data !

    Thanks in advance

    Hello,

    That automatic URL generation on variation isn’t part of how the core WooCommerce plugin works. If that’s indeed a must-have, you’d need to have that custom-developed by a web developer or customisation expert as seen as https://woocommerce.com/customizations/.

    If you have any further question(s), please let us know. 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 6 replies - 1 through 6 (of 6 total)

The ticket ‘URL Structured data’ is closed to new replies.