0

My code:

app\design\frontend\Zero\my_theme\Magento_Wishlist\templates\catalog\product\view\addto\wishlist.phtml

<?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>

CSS: app\design\frontend\Zero\my_theme\Magento_Wishlist\web\css_extend.less

.action.towishlist.already-added{color: #ff0000 !important;}

After the item added to the wishlist the wishlist icon color not update.

5
  • Replece CSS code :- .action.towishlist.already-added:before{color: #ff0000 !important;}
    – Himanshu
    Feb 10, 2021 at 13:35
  • No, not working.
    – zus
    Feb 10, 2021 at 13:50
  • @Himanshu I see one docs magento.stackexchange.com/a/266125/57334 can you pls help how to implement the controller?
    – zus
    Feb 10, 2021 at 13:58
  • I saw the link that i gave you, i am getting confused about how to implement the folder structure, can you pls guide me on the structure, where i can put my list.phtml, guide me if i am wrong --> app\design\frontend\Zero\my_theme\Magento_Catalog\templates\product\list.phtml rigjht?
    – zus
    Feb 10, 2021 at 14:09
  • Check My Answer.
    – Himanshu
    Feb 10, 2021 at 14:24

1 Answer 1

-1

Try:-

Path:-

app\design\frontend\Zero\my_theme\Magento_Wishlist\templates\catalog\product\view\addto\wishlist.phtml

     <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var \Magento\Wishlist\Block\Catalog\Product\View\AddTo\Wishlist $block */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$wishlist = $objectManager->get('\Magento\Wishlist\Model\Wishlist');
$customerSession = $objectManager->get('Magento\Customer\Model\SessionFactory')->create();
$customerData = $customerSession->getCustomer()->getData(); //get all data of customerData
$customerId = $customerSession->getCustomer()->getId();//get id of customer
$isAdded = $wishlist->loadByCustomerId($customerId, true)->getItemCollection();
?>
<style>
    .action.towishlist.already-added:before{color: #ff0000 !important;}
</style>
<?php if ($block->isWishListAllowed()) : ?>
    <?php if($isAdded->count()): ?>
        <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>

Add module.less code :-

Path:- app\design\frontend\Zero\my_theme\Magento_Wishlist\web\module.less

.action.towishlist.already-added:before{color: #ff0000 !important;}
7
  • Not working. Still, wishlist icon not changing the color.
    – zus
    Feb 10, 2021 at 14:28
  • share ScreenShot
    – Himanshu
    Feb 10, 2021 at 14:31
  • Code path : app\design\frontend\Zero\my_theme\Magento_Wishlist\templates\catalog\product\view\addto\wishlist.phtml Css Path: app\design\frontend\Zero\my_theme\Magento_Wishlist\web\css_extend.less
    – zus
    Feb 10, 2021 at 14:41
  • I am getting an error : 1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method Magento\Wishlist\Block\Catalog\Product\ProductList\Item\AddTo\Wishlist\Interceptor::isWishListAllowed
    – zus
    Feb 10, 2021 at 14:42
  • Hi himanshu, I hope my point clear, anything else i need to update
    – zus
    Feb 10, 2021 at 14:51

Your Answer

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

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