category page with snippet data and “offer”

#835555
  • Resolved Alireza
    Rank Math free

    Hi! I have a category page with products in it. I have submitted it to google search console and received the error “Either ‘offers’, ‘review’ or ‘aggregateRating’ should be specified”. Your colleague told me to Remove Snippet Data for my category pages. But now after 2 months of submitting the page without the snippet data, I believe my page is not properly indexed by google. Now, I’d like to add the snippet data with “offer” to my category pages. Can you please advise on how to proceed?

Viewing 8 replies - 16 through 23 (of 23 total)
  • Alireza
    Rank Math free

    Hi, thanks! This code is almost the same as the one I have. And it doesn’t add anything to the Color Page: mysite.com/colour/white

    This is what I have in my color page schema:

    {"@type":"CollectionPage","@id":"https://mysite.com/colour/white/#webpage","url":"https://mysite.com/colour/white/","name":"White","isPartOf":{"@id":"https://mysite.com/#website"},"inLanguage":"en"}

    And I’d like to add products with offer to this schema.

    Hello,

    Please note that the color schema property is only eligible for the product schema type. Since our goal is to remove the product schema from your category page to avoid this specific error (Either ‘offers’, ‘review’ or ‘aggregateRating’ should be specified), you will no longer need that property.

    Please refer to more about that from here: https://schema.org/color

    You should focus on adding that property to your single products instead.

    Don’t hesitate to get in touch with us if you have any other questions.

    Alireza
    Rank Math free

    Hi, thanks for the clarification. Can you please provide the filter to be added in my functions.php that can add color to the product schema in my individual product pages? Right now, I have a pa_colour which is added as an additional property, but I want to include that inside the product schema. I have tried to add a filter but it doesn’t change anything in the output. Can you check what the problem is?

    add_filter('rank_math/json_ld', function($data, $jsonld) {
        if (!is_product()) {
            return $data;
        }
    
        $product_id = get_the_ID();
        $product = wc_get_product($product_id);
    
        if (!$product) {
            error_log('Product not found for ID: ' . $product_id);
            return $data;
        }
    
        $colors = wc_get_product_terms($product_id, 'pa_colour', array('fields' => 'names'));
        $color_string = implode(', ', $colors);
    
        $materials = wc_get_product_terms($product_id, 'pa_pile-material', array('fields' => 'names'));
        $material_string = implode(', ', $materials);
    
        $thickness = $product->get_attribute('pa_thickness');
        $origin = $product->get_attribute('pa_origin');
        $weight = $product->get_attribute('pa_weight');
    
        if (isset($data['@graph']) && is_array($data['@graph'])) {
            foreach ($data['@graph'] as &$item) {
                if ($item['@type'] === 'Product') {
                    // Add color directly to the Product schema
                    $item['color'] = $color_string;
    
                    // Add material to the Product schema
                    if (!empty($material_string)) {
                        $item['material'] = $material_string;
                    }
    
                    // Add depth (thickness)
                    if (!empty($thickness)) {
                        $item['depth'] = [
                            "@type" => "QuantitativeValue",
                            "unitCode" => "MMT",
                            "value" => $thickness
                        ];
                    }
    
                    // Add countryOfOrigin
                    if (!empty($origin)) {
                        $item['countryOfOrigin'] = $origin;
                    }
    
                    // Add weight
                    if (!empty($weight)) {
                        $item['weight'] = [
                            "@type" => "QuantitativeValue",
                            "unitCode" => "KGM",
                            "value" => $weight
                        ];
                    }
    
                    // Update priceValidUntil if it exists
                    if (isset($item['offers']) && is_array($item['offers'])) {
                        $item['offers']['priceValidUntil'] = date('Y-m-d', strtotime('+1 year'));
                    }
    
                    // Ensure image is set
                    if (!isset($item['image']) || empty($item['image'])) {
                        $image_id = $product->get_image_id();
                        $image_url = wp_get_attachment_image_url($image_id, 'full');
                        if ($image_url) {
                            $item['image'] = $image_url;
                        }
                    }
    
                    // Remove additionalProperty
                    if (isset($item['additionalProperty'])) {
                        unset($item['additionalProperty']);
                    }
    
                    break;
                }
            }
        }
    
        return $data;
    }, 99999, 2);

    Hello,

    Can you please confirm if your product page with color variations is an actual Variable Product?

    We ask this because Rank Math should be able to generate the color property automatically in the schema so you don’t have to apply additional filter code.

    If that’s not the case on your end, please share the product page link here.

    Looking forward to helping you.

    Alireza
    Rank Math free

    My products are simple (one product per post), therefore I didn’t use the woocommerce color property, but added it manually with attribute pa_colour. Can you check why the code I provided doesn’t apply to my page? I added the link in the sensitive data section. Please don’t include my sitename in the discussion.

    Hello,

    Please try this method instead:

    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
    	$product_id = get_the_ID();
    	$colors = wc_get_product_terms($product_id, 'pa_colour', array('fields' => 'names'));
      $color_string = implode(', ', $colors);
    
    	$entity['color'] = $color_string;
    
    	return $entity;
    });

    Let us know how that goes.

    Looking forward to helping you.

    Alireza
    Rank Math free

    Thanks! This works. Now I want to remove certain parts of the markup added by rank math. What’s the rank math filter for giving priority to those? For example, I want to remove the additionalProperty, or assign correct values to height and width.

    Hello,

    There are two filters currently available to change the current Schema output and they are the following:
    https://rankmath.com/kb/filters-hooks-api-developer/#extend-json-ld-data
    https://rankmath.com/kb/filters-hooks-api-developer/#change-post-schema-data

    The previous filter we shared makes use of the second option to change the color attribute name being passed to the markup so you can either add more functionality to that by looking for the correct key in $entity array and changing the properties as per your needs.

    Don’t hesitate to get in touch if you have any other questions.

    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 8 replies - 16 through 23 (of 23 total)

The ticket ‘category page with snippet data and “offer”’ is closed to new replies.