GTIN numbers for variable products

#70018
  • Resolved Jason Jeffers
    Rank Math free

    We have a series of products each with variations in liters. For merchant centers, apparently we need to output individual offers within the Offers -> AggregateOffer attributes with @type: offer, price, priceCurrency, sku, name, availability and most importantly gtin13. I’ve seen variations of code to achieve parts of this equation but am struggling to output the proper format in a foreach loop. The only way I have found is to use the [$key] index entity. Unfortunately, this adds an index to the Offers -> AggregateOffers which of course is not a valid attribute:

    “offers”: {
    “0”: { // The property 0 is not recognized by Google for an object of type AggregateOffer.
    “offer”: {
    “@type”: “Offer”,
    “name”: “1 Liter”,
    “sku”: “1-liter”,
    “gtin13”: “7424903198115”,
    “price”: “19.29”,
    “priceCurrency”: “EUR”,
    “availability”: “InStock”
    }
    }, etc.

    Without the [$key] I can only output the last variation. The rest of the code gathers the necessary fields properly. Here is the complete code I am using:

    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
        global $product;
        $entity['gtin8'] = $product->get_sku();
        if ( $product->is_type( 'variable' ) ) {
            $variations = $product->get_available_variations();
            foreach ( $variations as $key => $variation ) {
                foreach ($variation['attributes'] as $attribute => $term_slug ) {
                    $taxonmomy = str_replace( 'attribute_', '', $attribute );
                    $name   = get_term_by( 'slug', $term_slug, $taxonmomy )->name;
                    $sku    = get_term_by( 'slug', $term_slug, $taxonmomy )->slug;
                    $gtin   = get_post_meta( $variation['variation_id'], 'custom_gtin_field', true );
                    $price  = $variation['display_regular_price'];
                    $price  = number_format((float)$price, 2, '.', '');
    
                    $entity['offers'][ $key ][ 'offer' ]['@type']           = 'Offer';
                    $entity['offers'][ $key ][ 'offer' ]['name']            = $name;
                    $entity['offers'][ $key ][ 'offer' ]['sku']             = $sku;
                    $entity['offers'][ $key ][ 'offer' ]['gtin13']          = $gtin;
                    $entity['offers'][ $key ][ 'offer' ]['price']           = $price;
                    $entity['offers'][ $key ][ 'offer' ]['priceCurrency']   = "EUR";
                    $entity['offers'][ $key ][ 'offer' ]['availability']    = "InStock";
                }
            }
        } else {
            $entity['gtin8'] = $product->get_sku();
        }
        return $entity;
    });

    As reference, here is one of the products on our site: https://www.ecoformeurope.com/product/cementmix-diy-make-waterproof-cement/

    Hopefully you can help getting this to output properly.

    Cheers,

    Jason Jeffers

Viewing 1 replies (of 1 total)
  • Hello,

    Thank you for contacting Rank Math today.

    I have already replied to your ticket on the same: https://support.rankmath.com/ticket/gtin-numbers-for-variable-products-2/?view=all

    For efficiency, I will close this ticket and we can continue any further inquiries in that ticket.

    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 1 replies (of 1 total)

The ticket ‘GTIN numbers for variable products’ is closed to new replies.