5

In Magento 1.x I did it like this:

$_product = ...; // some product object you already have
$_productCollection = Mage::helper('wishlist')->getProductCollection()
    ->addFieldToFilter('sku', $_product->getSku());
if($_productCollection->count() > 0) {
    // User already has item in wishlist.
}

How can I achieve the same in Magento 2?

2

As far as I know, we can use \Magento\Wishlist\Helper\Data to check if a product is already on the wishlist.

/** @var \Magento\Wishlist\Helper\Data $wishlistHelper **/

$wishlistHelper->getWishlistItemCollection()
              ->addFieldToFilter('product_id', $_product->getId());

Remember to inject this class in your construction.

| improve this answer | |
1

Try this code..

<?php
$wishlistHelper = $this->helper('Magento\Wishlist\Helper\Data');

if($wishlistHelper->isAllow()){

$wishlistCollection = $wishlistHelper->getWishlistItemCollection();     
foreach ($wishlistCollection as $_wishlist_item) {
   $in_wishlist = false;
   if ($_product->getId() == $_wishlist_item->getProduct()->getId()) {
       $in_wishlist = true; break;
   }
}
?>

if(!empty($_in_wishlist) && $_in_wishlist){ ?>
  <li><span class="in-wishlist"><?= __('Already in Wishlist')?></span></li>
<?php }else{ ?>
  <li><a href="<?= $wishlistHelper->getAddUrl($_product) ?>" class="link-wishlist"><?= __('Add to Wishlist') ?></a></li>
<?php } ?>
<?php } ?>
| improve this answer | |
1
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_in_wishlist = false;
foreach ($this->helper('Magento\Wishlist\Helper\Data')->getWishlistItemCollection() as $_wishlist_item){
    if($_product->getId() == $_wishlist_item->getProduct()->getId()){
        $_in_wishlist = true;
    }
}
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) 
{
    if($_in_wishlist) { ?>
        <a href="#" data-post="#" data-action="already-in-wishlist"><span class="wish-list lineheight48"><img data-alt-src="<?php echo $block->getViewFileUrl('images/icons/body-shop-icons_wishlist_filled.svg'); ?>" width="38" src='<?php echo $this->getViewFileUrl('images/icons/icon-wishlist-black.svg'); ?>' alt="Wish list"></span></a>
    <?php } else { ?>
        <a href="#" data-post='<?php echo $this->helper('Magento\Wishlist\Helper\Data')->getAddParams($_product) ?>' class="action towishlist" data-action="add-to-wishlist"><span class="wish-list lineheight48"><img data-alt-src="<?php echo $block->getViewFileUrl('images/icons/body-shop-icons_wishlist_filled.svg'); ?>" width="38" src='<?php echo $this->getViewFileUrl('images/icons/icon-wishlist.svg'); ?>' alt="Wish list"></span></a>
    <?php } ?>
    <?php 
} else { ?> 

    <a href="#" data-post='<?php echo $this->helper('Magento\Wishlist\Helper\Data')->getAddParams($_product) ?>' class="action towishlist" data-action="add-to-wishlist"><span class="wish-list lineheight48"><img data-alt-src="<?php echo $block->getViewFileUrl('images/icons/body-shop-icons_wishlist_filled.svg'); ?>" width="38" src='<?php echo $this->getViewFileUrl('images/icons/icon-wishlist.svg'); ?>' alt="Wish list"></span></a>
<?php
}
?>
| improve this answer | |
0

In other way you can get wishlist items collection.

Use factory Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory for getting wishlist product collection

Use Magento\Catalog\Api\ProductRepositoryInterface to get it in your constructor

and filter that by addCustomerIdFilter($customerId)

protected $_currentUserWishlistCollectionFactory ;
protected $_Customersession;
private $productRepository;     
public function __construct(
    \Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory $currentUserWishlistCollectionFactory,
    \Magento\Customer\Model\Session $Customersession,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository

    ) {
    ....
    $this->_Customersession = $Customersession;
$this->_currentUserWishlistCollectionFactory = $currentUserWishlistCollectionFactory;
$this->productRepository = $productRepository;
....

    }

public function getcurrentUserWishlistItems(){
    $productId  = false;
    $collection = $this->_currentUserWishlistCollectionFactory->create();
$product = $this->loadMyProduct($_product->getSku());
$productId= $product->getId();
    $collection->addCustomerIdFilter($this->_Customersession->getCustomerId());
$collection->addFieldToFilter('product)_id',$productId  );
    return $collection;

}

public function loadMyProduct($sku)
{
    return $this->productRepository->get($sku);
}
| improve this answer | |
  • Thanks for your answer but I was searching for quick answer. – user2737980 Jan 4 '17 at 7:21
  • ah.. you already got from Khoa TruongDinh – Amit Bera Jan 4 '17 at 7:22
0

You can use this code to check if a product is already on the wishlist in Magento2:

<?php

    //Checking User logedin or not          
    $objectManager       = \Magento\Framework\App\ObjectManager::getInstance();
    $context             = $objectManager->get('Magento\Framework\App\Http\Context');
    $isLoggedIn          = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
    $customerSession     = $objectManager->create("Magento\Customer\Model\Session");
    if($customerSession->isLoggedIn()){
      //getting customer ID
      $id =  $customerSession->getCustomerID();
    }

    //Getting wishlist products Collections  Related to Customers
    $wishlist            = $objectManager->get('\Magento\Wishlist\Model\Wishlist');
    $wishlist_collection = $wishlist->loadByCustomerId( $id , true)->getItemCollection();
    $_in_wishlist        = "false";
    foreach ($wishlist_collection as $wishlist_product):
      if($_product->getId() == $wishlist_product->getProduct()->getId()){
        $_in_wishlist = "true";
        break;
      }
      endforeach;
      if($_in_wishlist == "true"): ?>
        <div class="addtowishlist added"  style="font-family: Raleway"><span>Already Added to Wishlist:</span><div data-role="add-to-links" class="actions-secondary"<?= strpos($pos, $viewMode . '-secondary') ? $position : '' ?>>
        <?php if ($addToBlock = $block->getChildBlock('addto')): ?>
          <?= $addToBlock->setProduct($_product)->getChildHtml() ?>
        <?php endif; ?>
             </div>
        </div>
        <?php else: ?>
          <div class="addtowishlist"  style="font-family: Raleway"><span>Add to Wishlist:</span><div data-role="add-to-links" class="actions-secondary"<?= strpos($pos, $viewMode . '-secondary') ? $position : '' ?>>
          <?php if ($addToBlock = $block->getChildBlock('addto')): ?>
            <?= $addToBlock->setProduct($_product)->getChildHtml() ?>
          <?php endif; ?>
             </div>
          </div>
        <?php endif; ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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