Magento – Structured data – Magento 2, 8 warnings

googlemagento2structured-data

I am using Magento 2,
I used the structed data testing tools and all of my 600+ products have 8 warnings of which are some important ones such as image, availability, url

How do I correct this?

Some of the fields such as price, name etc are completed

Best Answer

You can add below code in one file (catalog_product_view handler) and make adjustments accordingly. It will help to add all options of structured data. Hope this helps

$_product = $this->getProduct();
$summaryModel = $block->getReviewSummary();
$reviewCount = $summaryModel->getReviewsCount();
if (!$reviewCount) {
    $reviewCount = 0;
}
$ratingSummary = ($summaryModel->getRatingSummary()) ? $summaryModel->getRatingSummary() : 20;

<div itemscope itemtype="http://schema.org/Product">
    <meta itemprop="name" content="<?php /* @escapeNotVerified */ echo $block->escapeQuote($block->stripTags($_product->getName())); ?>" />
    <meta itemprop="image" content="<?php /* @escapeNotVerified */ echo $block->stripTags($block->getImage($_product, 'product_base_image')->getImageUrl()); ?>" />
    <meta itemprop="description" content="<?php /* @escapeNotVerified */ echo $block->stripTags($_product->getDescription()); ?>" />
    <meta itemprop="url" content="<?php /* @escapeNotVerified */ echo $block->stripTags($_product->getProductUrl()); ?>" />
    <meta itemprop="sku" content="<?php /* @escapeNotVerified */ echo $block->stripTags($_product->getSku()); ?>" />
    <div itemtype="http://schema.org/AggregateRating" itemscope itemprop="aggregateRating">
        <meta itemprop="worstRating" content="1" />
        <meta itemprop="bestRating" content="5" />
        <meta itemprop="ratingValue" content="<?php echo $ratingSummary / 20 ; ?>" />
        <meta itemprop="reviewCount" content="<?php echo $reviewCount; ?>" />
    </div>
    <div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
        <meta itemprop="priceCurrency" content="<?php echo $block->getCurrencyCode() ?>" />
        <meta itemprop="price" content="<?php echo $_product->getFinalPrice(); ?>" />
        <?php if ($_product->isAvailable()): ?>
            <link itemprop="availability" href="http://schema.org/InStock" />
        <?php else : ?>
            <link  itemprop="availability" href="http://schema.org/OutOfStock" />
        <?php endif ?>
    </div>
</div>
Related Topic