Forked from denchev/AddImageToCompareProductsPlugin.php
Created
February 14, 2019 15:44
-
-
Save rubenRP/002d06c7b8d6c6eb44d43e102f86ec38 to your computer and use it in GitHub Desktop.
Magento 2 show image in sidebar compare list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\Namespace\Plugin; | |
use Magento\Catalog\Helper\ImageFactory; | |
use Magento\Catalog\Helper\Product\Compare; | |
use Magento\Catalog\Model\ProductRepository; | |
class AddImageToCompareProductsPlugin | |
{ | |
protected $helper; | |
protected $imageHelperFactory; | |
protected $productRepository; | |
public function __construct( | |
Compare $helper, | |
ImageFactory $imageHelperFactory, | |
ProductRepository $productRepository | |
) | |
{ | |
$this->helper = $helper; | |
$this->imageHelperFactory = $imageHelperFactory; | |
$this->productRepository = $productRepository; | |
} | |
public function afterGetSectionData(\Magento\Catalog\CustomerData\CompareProducts $subject, $result) | |
{ | |
$images = []; | |
foreach ($this->helper->getItemCollection() as $item) { | |
$imageHelper = $this->imageHelperFactory->create(); | |
try { | |
$product = $this->productRepository->getById($item->getId()); | |
$images[$item->getId()] = $imageHelper->init($product, 'recently_compared_products_grid_content_widget')->getUrl(); | |
} catch (\Exception $ex) { | |
$images[$item->getId()] = $imageHelper->getDefaultPlaceholderUrl(); | |
} | |
} | |
$items = $result['items']; | |
foreach ($items as &$item) { | |
$item['image_src'] = $images[$item['id']]; | |
} | |
$result['items'] = $items; | |
return $result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<!-- | |
/** | |
* Copyright © Magento, Inc. All rights reserved. | |
* See COPYING.txt for license details. | |
*/ | |
--> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | |
<type name="Magento\Catalog\CustomerData\CompareProducts"> | |
<plugin name="addImageToCompareProducts" type="Vendor\Namespace\Plugin\AddImageToCompareProductsPlugin" /> | |
</type> | |
</config> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing!