How to set an active class to magento2.1 wish-list button on list page? I mean that, when i go to product page, if marked that product to my wish-list before, just shows already added to wish-list or change the color of the button. I used below code on the product page and its ok but not works on the category page
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/** @var \Magento\Wishlist\Block\Catalog\Product\View\AddTo\Wishlist $block */
?>
<?php if ($block->isWishListAllowed()) : ?>
<?php $_product = $block->getProduct(); ?>
<?php $isAdded = $this->helper('Magento\Wishlist\Helper\Data')->getWishlistItemCollection()->addFieldToFilter('product_id', $_product->getId())->count(); ?>
<?php if($isAdded): ?>
<a href="javascript:void(0)" class="action towishlist already-added" data-action="add-to-wishlist"><span><?= $block->escapeHtml(__('Added to Wish List')) ?></span></a>
<?php else: ?>
<a href="#" class="action towishlist" data-post='<?= /* @noEscape */ $block->getWishlistParams() ?>' data-action="add-to-wishlist"><span><?= $block->escapeHtml(__('Add to Wish List')) ?></span></a>
<?php endif; ?>
<?php endif; ?>
<script type="text/x-magento-init">
{
"body": {
"addToWishlist": <?= /* @noEscape */ $block->getWishlistOptionsJson() ?>
}
}
</script>