0

I'm looking to add all media images to structured data for the product page.

currently, it only adds one image to the structured data via

'image'       => $this->getUrl('pub/media/catalog') . 'product' . $currentProduct->getImages(),

I tried it this way but didn't seem to work.

'image'       => $this->getUrl('pub/media/catalog') . 'product' . $currentProduct->getMediaGalleryImages(),

or create an gallery images array

$productimages = array(); //test
$productimages = $currentProduct->getMediaGalleryImages();//test
'image'       => $this->getUrl('pub/media/catalog') . 'product' . $productimages,

the multiple images should be send in an array like this

"image": [
  "https://example.com/photos/1x1/photo.jpg",
  "https://example.com/photos/4x3/photo.jpg",
  "https://example.com/photos/16x9/photo.jpg"
]

Here is part of the code

public function showProductStructuredData()
{
    if ($currentProduct = $this->getProduct()) {
        try {
            $productId = $currentProduct->getId() ? $currentProduct->getId() : $this->request->getParam('id');

            $product = $this->productFactory->create()->load($productId);
            $availability = $product->isAvailable() ? 'InStock' : 'OutOfStock';
            $productimages = array(); //test
            $productimages = $currentProduct->getMediaGalleryImages();//test
            $stockItem = $this->stockState->getStockItem(
                $product->getId(),
                $product->getStore()->getWebsiteId()
            );
            $priceValidUntil = $currentProduct->getSpecialToDate();
            $productStructuredData = [
                '@context'    => 'http://schema.org/',
                '@type'       => 'Product',
                'name'        => $currentProduct->getName(),
                'description' => trim(strip_tags($currentProduct->getShortDescription())),
                'sku'         => $currentProduct->getSku(),
                'mpn'         => $currentProduct->getSku(), //added 7-30-2019               
                'url'         => $currentProduct->getProductUrl(),
                'image'       => $this->getUrl('pub/media/catalog') . 'product' . $currentProduct->getImages(),
                'brand'       => 'mybrand', //added 7-30-2019
                'offers'      => [
                    '@type'         => 'Offer',
                    'priceCurrency' => $this->_storeManager->getStore()->getCurrentCurrencyCode(),
                    'price'         => $currentProduct->getPriceInfo()->getPrice('final_price')->getValue(),
                    'itemOffered'   => $stockItem->getQty(),
                    'itemCondition' => 'New', //added 7-30-2019
                    'url'           => $currentProduct->getProductUrl(), //added 7-30-2019
                    'availability'  => 'http://schema.org/' . $availability
                ]
            ];
            $productStructuredData = $this->addProductStructuredDataByType($currentProduct->getTypeId(), $currentProduct, $productStructuredData);

            if (!empty($priceValidUntil)) {
                $productStructuredData['offers']['priceValidUntil'] = $priceValidUntil;
            }

            if ($this->getReviewCount()) {
                $productStructuredData['aggregateRating']['@type'] = 'AggregateRating';
                $productStructuredData['aggregateRating']['bestRating'] = 100;
                $productStructuredData['aggregateRating']['worstRating'] = 0;
                $productStructuredData['aggregateRating']['ratingValue'] = $this->getRatingSummary();
                $productStructuredData['aggregateRating']['reviewCount'] = $this->getReviewCount();
            }

            $objectStructuredData = new \Magento\Framework\DataObject(['mpdata' => $productStructuredData]);
            $this->_eventManager->dispatch('mp_seo_product_structured_data', ['structured_data' => $objectStructuredData]);
            $productStructuredData = $objectStructuredData->getMpdata();

            return $this->helperData->createStructuredData($productStructuredData, '<!-- Product Structured Data by Mageplaza SEO-->');
        } catch (\Exception $e) {
            $this->messageManager->addError(__('Can not add structured data'));
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.