We’re locked at home because of covid-19, but working to support your business.
We apologize if you face any delay to get support response.
Stay safe and take care of your family and yourself.

Hi,

I’m using a generate variation URL for each variation in my product page. I would like to add these URL in structured data, How can I make that please ?

This my code to generate structured data :

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;
} );

Thanks in advance

Ahmed Ehsaan

Hi Thibaut,

The URL is coming from WooCommerce default.

Is that code working with default system: https://www.loom.com/share/fd4e7f1ca98145279d15c82f7b98641d

Thank You

Hi Ahmed,

Can you give the me the right code please ?

Thanks in advance

Tanvirul Haque

Hi Thibaut,

Unfortunately, URL rewrite is not the feature of our plugin, it is the default of WooCommerce itself and we just visible it. So we have tried to reproduce it by deactivating our plugin, please see this video https://www.loom.com/share/fd4e7f1ca98145279d15c82f7b98641d

Could you please disable our swatches plugins and check is your code working without our plugin or not?

Thank you

Hi Tanvirul,

I understand it’s not your plug in.

But I’m activated this option thanks to your plug in.

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

Can you give the right function ? In order to have an URL for each variation.
Thanks in advance

Hi,

To answer of your video, before I selected “Generate sharable url based on selected variation attributes.” I didn’t have an URL for each variation.
But I think you don’t use get_permalink because in structured date, these URL don’t appear.

Thanks in advance

Hi,

According to woocommerce :

”That automatic URL generation on variation isn’t part of how the core WooCommerce plugin works. ”

So, can you help me please ?

These URL are generated by your plug in.

Regards,

 

Ahmed Ehsaan

Hi Thibaut,

Developer will return on 16th August. I’ll let you know over Skype.

Thank You