0

I'm in Magento2,

All I need to do is get all the images of the products in the Wishlist Page.

This is my app/design/frontend/<Vendor>/<theme>/Magento_Wishlist/templates/item/list.phtml

<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

use Magento\Framework\Escaper;
use Magento\Wishlist\Block\Customer\Wishlist\Items;

/** @var Escaper $escaper */
/** @var Items $block */
$columns = $block->getColumns();
?>

    <div class="products-grid wishlist">
        <?php if (count($block->getItems())): ?>
            <ol class="product-items flex flex-row flex-wrap">
                <?php foreach ($block->getItems() as $item): ?>
                    <li data-row="product-item" class="product-item w-full sm:w-1/2 xl:w-1/3 flex"
                        id="item_<?= $escaper->escapeHtmlAttr($item->getId()) ?>">
                        <div class="product-item-info mr-2 mb-2 w-full" data-container="product-grid">
                            <?php foreach ($columns as $column): ?>
                                <?= $column->setItem($item)->toHtml();?>
                            <?php endforeach; ?>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ol>
        <?php else: ?>
            <div class="message info empty">
                <span><?= $escaper->escapeHtml(__('This Wish List has no Items')) ?></span>
            </div>
        <?php endif; ?>
    </div>

<?php foreach ($columns as $column): ?>
    <?= $column->getAdditionalHtml() ?>
<?php endforeach; ?>

This is my app/design/frontend/<Vendor>/<theme>/Magento_Wishlist/templates/item/column/image.phtml

<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

use Magento\Framework\Escaper;
use Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Image;
use Magento\Wishlist\Model\Item;

/** @var Image $block */
/** @var Escaper $escaper */

/** @var Item $item */
$item = $block->getItem();
$product = $item->getProduct();

?>
<a class="product-item-photo w-full flex mb-2" tabindex="-1"
   href="<?= $escaper->escapeUrl($block->getProductUrl($item)) ?>"
   title="<?= $escaper->escapeHtmlAttr($product->getName()) ?>">
    <?= $block->getImage($block->getProductForThumbnail($item), 'wishlist_thumbnail')->toHtml() ?>
</a>

If I'm not wrong, use Magento\Wishlist\Block\Customer\Wishlist\Items; and the variable $items allows me to get all the product in the Wishlist, but the media gallery is apparently missing.

There's a way I can get the complete gallery in the Wishlist Page?

Thanks guys!

1 Answer 1

0

Can you try below code and check.

The image gallery is added to the product via an extension interface defined via di.xml. The upshot is that we can manually create an instance of the gallery ReadHandler class and pass a product to load all its gallery images.

As usual in Magento 2 the best way to instantiate a class is via the __construct() method, so here is a stub block class:

First of all override your file.which is image is rendering Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Image.php. And then add below code.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Wishlist\Block\Customer\Wishlist\Item\Column;

use Magento\Catalog\Model\Product\Image\UrlBuilder;
use Magento\Framework\View\ConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler;

/**
 * Wishlist block customer item cart column
 *
 * @api
 * @since 100.0.2
 */
class Image extends \Magento\Wishlist\Block\Customer\Wishlist\Item\Column
{
    /** @var ItemResolverInterface */
    private $itemResolver;

    /**
     * @var GalleryReadHandler
     */
    protected $galleryReadHandler;

    /**
     * @param \Magento\Catalog\Block\Product\Context $context
     * @param \Magento\Framework\App\Http\Context $httpContext
     * @param array $data
     * @param ConfigInterface|null $config
     * @param UrlBuilder|null $urlBuilder
     * @param ItemResolverInterface|null $itemResolver
     */
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = [],
        ConfigInterface $config = null,
        UrlBuilder $urlBuilder = null,
        ItemResolverInterface $itemResolver = null,
        GalleryReadHandler $galleryReadHandler
    ) {
        $this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
        $this->galleryReadHandler = $galleryReadHandler;
        parent::__construct(
            $context,
            $httpContext,
            $data,
            $config,
            $urlBuilder
        );
    }

    /**
     * Identify the product from which thumbnail should be taken.
     *
     * @return \Magento\Catalog\Model\Product
     * @since 101.0.5
     */
    public function getProductForThumbnail(\Magento\Wishlist\Model\Item $item) : \Magento\Catalog\Model\Product
    {
        return $this->itemResolver->getFinalProduct($item);
    }
    
    /** Add image gallery to $product */
    public function addGallery($product)
    {
        $this->galleryReadHandler->execute($product);
    }
}

In above code i have add new class is Magento\Catalog\Model\Product\Gallery\ReadHandler for create an instance of the gallery ReadHandler class and pass a product to load all its gallery images.

And then image.phtml file add below code and get all images of products.

<?php
$block->addGallery($product);
$images = $product->getMediaGalleryImages();
foreach ($images as $image) {
    echo $image['url'] . "</br>";
}
die;
?>

And See output

enter image description here Thanks.

3
  • Thanks man, I'm trying right now :) I'll let you know!
    – D.D.
    Commented Jun 20, 2022 at 6:37
  • Where I can override this Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Image.php? in app/code?
    – D.D.
    Commented Jun 22, 2022 at 9:06
  • Yes, your custom module in app/code folder. And check how to override block meetanshi.com/blog/override-block-model-controller-magento-2 Commented Jun 22, 2022 at 16:50

Your Answer

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

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