乐趣区

关于magento2: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; ?>
退出移动版