check if a product is already on the wishlist in Magento2

亲测有效

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"): ?>
        
Already Added to Wishlist:
> getChildBlock('addto')): ?> setProduct($_product)->getChildHtml() ?>
Add to Wishlist:
> getChildBlock('addto')): ?> setProduct($_product)->getChildHtml() ?>

你可能感兴趣的:(magento2)