You can do it by plugin or overwrite of **Magento\Wishlist\CustomerData\Wishlist**
. I will cover pluginize **getSectionData**
for achieving this.
VendorName/ModuleName/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Wishlist\CustomerData\Wishlist">
<plugin name="customer_data_wishlist" type="VendorName\ModuleName\Plugin\Wishlist\CustomerData\Wishlist" sortOrder="1"/>
</type>
</config>
VendorName/ModuleName/Plugin/Wishlist/CustomerData/Wishlist.php
> namespace VendorName\ModuleName\Plugin\Wishlist\CustomerData;
class Wishlist
{
/**
* @var \Magento\Wishlist\Helper\Data
*/
protected $wishlistHelper;
/**
* @var \Magento\Catalog\Helper\ImageFactory
*/
protected $imageHelperFactory;
/**
* @var \Magento\Framework\App\ViewInterface
*/
protected $view;
/**
* @var \Magento\Wishlist\Block\Customer\Sidebar
*/
protected $block;
/**
* Wishlist constructor.
*
* @param \Magento\Wishlist\Helper\Data $wishlistHelper
* @param \Magento\Wishlist\Block\Customer\Sidebar $block
* @param \Magento\Catalog\Helper\ImageFactory $imageHelperFactory
* @param \Magento\Framework\App\ViewInterface $view
*/
public function __construct(
\Magento\Wishlist\Helper\Data $wishlistHelper,
\Magento\Wishlist\Block\Customer\Sidebar $block,
\Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
\Magento\Framework\App\ViewInterface $view
) {
$this->wishlistHelper = $wishlistHelper;
$this->imageHelperFactory = $imageHelperFactory;
$this->block = $block;
$this->view = $view;
}
public function aroundGetSectionData(
\Magento\Wishlist\CustomerData\Wishlist $subject,
\Closure $proceed
) {
$counter = $this->getCounter();
return [
'counter' => $counter,
'items' => $counter ? $this->getItems() : [],
];
}
/**
* @return string
*/
protected function getCounter()
{
return $this->createCounter($this->wishlistHelper->getItemCount());
}
/**
* Create button label based on wishlist item quantity
*
* @param int $count
* @return \Magento\Framework\Phrase|null
*/
protected function createCounter($count)
{
if ($count > 1) {
return __('%1 items', $count);
} elseif ($count == 1) {
return __('1 item');
}
return null;
}
/**
* Get wishlist items
*
* @return array
*/
protected function getItems()
{
$this->view->loadLayout();
$collection = $this->wishlistHelper->getWishlistItemCollection();
$collection->clear()->setInStockFilter(true)->setOrder('added_at');
$items = [];
foreach ($collection as $wishlistItem) {
$items[] = $this->getItemData($wishlistItem);
}
return $items;
}
/**
* Retrieve wishlist item data
*
* @param \Magento\Wishlist\Model\Item $wishlistItem
* @return array
*/
protected function getItemData(\Magento\Wishlist\Model\Item $wishlistItem)
{
$product = $wishlistItem->getProduct();
return [
'image' => $this->getImageData($product),
'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem),
'product_name' => $product->getName(),
'product_price' => $this->block->getProductPriceHtml(
$product,
'wishlist_configured_price',
\Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
['item' => $wishlistItem]
),
'product_is_saleable_and_visible' => $product->isSaleable() && $product->isVisibleInSiteVisibility(),
'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product),
'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem, true),
'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem, true),
];
}
/**
* Retrieve product image data
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Block\Product\Image
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function getImageData($product)
{
/** @var \Magento\Catalog\Helper\Image $helper */
$helper = $this->imageHelperFactory->create()
->init($product, 'wishlist_sidebar_block');
$template = $helper->getFrame()
? 'Magento_Catalog/product/image'
: 'Magento_Catalog/product/image_with_borders';
$imagesize = $helper->getResizedImageInfo();
$width = $helper->getFrame()
? $helper->getWidth()
: (!empty($imagesize[0]) ? $imagesize[0] : $helper->getWidth());
$height = $helper->getFrame()
? $helper->getHeight()
: (!empty($imagesize[1]) ? $imagesize[1] : $helper->getHeight());
return [
'template' => $template,
'src' => $helper->getUrl(),
'width' => $width,
'height' => $height,
'alt' => $helper->getLabel(),
];
}
}
Clear cache.
NB: Only refresh is not enough for testing working or not. Add/Remove wishlist item then magento fire sidebar section data and load.