0

For one product I am getting gallery images 0.

in gallery.phtml

<?php $galleryImages = $block->getGalleryImages(); ?>

gives me count 0. I checked it database table catalog_product_entity_media_gallery_value_to_entity for row_id which gives me 7 value_id(which means 7 images) which is linked to catalog_product_entity_media_gallery.

Whats the reason. How can i get all media gallery images.

Gallery.php

 public function getGalleryImages()
{
    $product = $this->getProduct();
    $images = $product->getMediaGalleryImages();

    \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class)->info("in images " .$images );


    if (!$images instanceof \Magento\Framework\Data\Collection) {
        return $images;
    }

    foreach ($images as $image) {
        $galleryImagesConfig = $this->getGalleryImagesConfig()->getItems();
        foreach ($galleryImagesConfig as $imageConfig) {
            $image->setData(
                $imageConfig->getData('data_object_key'),
                $this->imageUrlBuilder->getUrl($image->getFile(), $imageConfig['image_id'])
            );
        }
    }

    return $images;
}

getMediaGalleryImages in file Product.php

 public function getMediaGalleryImages()
{
    $directory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
    if (!$this->hasData('media_gallery_images')) {
        $this->setData('media_gallery_images', $this->_collectionFactory->create());
    }
    if (!$this->getData('media_gallery_images')->count() && is_array($this->getMediaGallery('images'))) {
        $images = $this->getData('media_gallery_images');
        foreach ($this->getMediaGallery('images') as $image) {
            if (!empty($image['disabled'])
                || !empty($image['removed'])
                || empty($image['value_id'])
                || $images->getItemById($image['value_id']) != null
            ) {
                continue;
            }
            $image['url'] = $this->getMediaConfig()->getMediaUrl($image['file']);
            $image['id'] = $image['value_id'];
            $image['path'] = $directory->getAbsolutePath($this->getMediaConfig()->getMediaPath($image['file']));
            $images->addItem(new \Magento\Framework\DataObject($image));
        }
        $this->setData('media_gallery_images', $images);
    }

    return $this->getData('media_gallery_images');
}

1 Answer 1

0

I don't think getMediaGalleryImages giving wrong data.

The MySQL query which you have run need additional where conditions for some fields.

So, You have to add store_id = YOURSTOREID and disabled = 0 and your condition. MYSQL Query is:

SELECT * FROM `catalog_product_entity_media_gallery_value` where store_id = YOUR_STORE_ID and disabled = 0

See the condition at getMediaGalleryImages in file Product.php

if (!empty($image['disabled'])
                || !empty($image['removed'])
                || empty($image['value_id'])
                || $images->getItemById($image['value_id']) != null
            ) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.