Add active class when product is in wishlist at category page
-
28-02-2021 - |
Question
I'm trying to add an active class when product is in wishlist.
I tried below code.
<?php
echo $_product->getId();
$wishlist = $this->helper('Magento\Wishlist\Helper\Data')->getWishlistItemCollection()->addFieldToFilter('product_id', $_product->getId());
print_r($wishlist->getData());
?>
<div class="cdz-product-wishlist show-tooltip">
<?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow() && $showCompareWishlist): ?>
<a href="#"
class="action towishlist"
title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_product); ?>'
data-action="add-to-wishlist"
role="button">
<span><?php /* @escapeNotVerified */ echo __('Add to Wish List') ?></span>
</a>
<?php endif; ?>
</div>
If I dump collection data then it returns me all products which are currently in wishlist but after I filter collection with product_id
then it returns always first array.
But product ID is printing correctly.
Does anyone has idea what's issue here ?
Solution
After spending an hour at last I ended up creating array of wishlist
products and match current product id using in_array.
Still if you have better solution I'm open for it.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange