Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5489

How to get all images (hidden/disabled) using product id in Magento 2

$
0
0

You will get the all product images with hidden or disabled by admin show in frontend.

Sometime we need all product images on front end but when we used function getMediaGalleryImages those only show non hidden/disabled images those disabled by admin end.

You can get the details of products images like filename, media_type, position,disabled and types.

We’re using product Repository for getting for all images of product

<?php
namespace Webkul\Test\Block\Image;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Catalog\Api\ProductRepositoryInterface;

class Images extends Template
{
    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;

    public function __construct(
        Context $context,
        ProductRepositoryInterface $productRepository,
        array $data = []
    ) {
        $this->productRepository = $productRepository;
        parent::__construct($context,$data);
    }

    /**
     * get All images
     */
    public function getImages()
    {
        $productId = 1;
        $productDetails = $this->productRepository->getById($productId);
        $images = $productDetails->getMediaGalleryEntries();
        foreach ($images as $image) {
         echo "<pre>";print_r($image->getData());
        }
    }

You will get the output

Array
(
    [file] => /w/s/wsh12-green_main_2.jpg
    [media_type] => image
    [entity_id] => 2046
    [label] => 
    [position] => 1
    [disabled] => 0
    [types] => Array
        (
            [0] => image
            [1] => small_image
            [2] => thumbnail
        )

    [id] => 3420
)
Array
(
    [file] => /w/s/wsh12-green_alt1_2.jpg
    [media_type] => image
    [entity_id] => 2046
    [label] => 
    [position] => 2
    [disabled] => 0
    [types] => Array
        (
        )

    [id] => 3421
)
Array
(
    [file] => /w/s/wsh12-green_back_2.jpg
    [media_type] => image
    [entity_id] => 2046
    [label] => 
    [position] => 3
    [disabled] => 0
    [types] => Array
        (
        )

    [id] => 3422
)

That’s all

If any issue or doubt please feel free to mentioned in comment section.

I would be happy to help.

Happy Coding!!! 🙂


Viewing all articles
Browse latest Browse all 5489

Trending Articles