SKU Offer for each variation

#75381
  • 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 do have a GTIN code for each offer and therefore each variation. Likewise, I would like to have a SKU code for each of the offers and variations. Everything is already well filled in woocommerce.

    Thanks in advance

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

    Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    Usually, we are not able to offer code customization since we are a small team, but if you have any particular questions, we will be happy to help you.

    About your issue, adding the SKU like that could help you:

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

    Looking forward to helping you. Thank you.

    ​​​​​​​

    Hi Alberto,

    Thanks for your reply ! I’m sorry but when I added your line, I have a critical error in my website. I’m added this line :

    $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 ),
                                    'sku' => $variation['variation_id']->get_sku(),
    			];
    		}
    	}

    All my SKU are filled in this field : https://prnt.sc/tuggut

    If I analyse, it seems to be this function : variable_sku[3] or variable_sku3

    I have to add variable_sku in your line or not please?

    Thanks in advance

    Alberto
    Rank Math business

    Hello,

    It seems you are using some custom meta fields for SKU and so on. In that case, I guess you will need to get it like you get the gtin13 and you will need to assign the sku as shown in my previous code (to the key ‘sku’).

    Looking forward to help you.

    Hi Alberto,

    I tried this following code but it’s not good :

    'sku' => $variation['variation_id']->get_sku(variable_sku),

    Can you help me please ?

    Thanks in advance

    It’s ok, it’s good with this code :
    'sku' => get_post_meta($variation['variation_id'],'variable_sku', true ),

    Hello,

    We are super happy that this resolved your issue.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

The ticket ‘SKU Offer for each variation’ is closed to new replies.