开发者

Check whether a product is in the wishlist or not

开发者 https://www.devze.com 2023-03-20 23:50 出处:网络
I\'m working on a Magento theme, and I need to build a function that can check to see whether or not a product has been added to the user\'s wishlist.

I'm working on a Magento theme, and I need to build a function that can check to see whether or not a product has been added to the user's wishlist.

Magento has a "Mage_Wishlist_Helper_Data" helpe开发者_开发问答r class, but I have no idea how to build a check-if-already-in-wishlist function. Basically I need to use Magento's wishlist feature to build a favorites list. I want to add a special class to the "add to wishlist" link if the particular product was already added to the user's favorites.


 <?php $wishlist = Mage::getModel('wishlist/item')->load($_product->getId(),'product_id');
      if($wishlist->getId())
          //product is added
      echo "Added! - Product is in the wishlist!";
      else
          //add product to wishlist
      echo "<a href='".$this->helper('wishlist')->getAddUrl($_product) ."'>Add This?</a>";
  ;?>


Since collections are lazy loaded, I am assuming you can do something such as:

$_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.
}

You can do similar filtering on other fields, but SKU should be sufficient in this case.


I'm only getting back to this project now, but I took Daniel Sloof's suggestion, and it worked perfectly using the function below:

public function isInWishlist($item)
{
    $_productCollection = Mage::helper('wishlist')->getProductCollection()
        ->addFieldToFilter('sku', $item->getSku());

    if($_productCollection->count()) {
        return true;
    }
    return false;
}

This checks to see whether or not a product has been added into the current user's Wishlist. I called it my template file like this:

if ($this->helper('wishlist')->isInWishlist($_product)) :


I found this solution after checked select query of Mage::helper('wishlist')->getWishlistItemCollection(). I hope this solution help to someone.

   /**
     * Check customers wishlist on identity product.
     * @param Mage_Catalog_Model_Product $_product
     * @return bool
     */
    private function _isInWishlist($_product)
    {        
        $_productCollection = Mage::helper('wishlist')->getWishlistItemCollection()
        ->addFieldToFilter('product_id', $_product->getId());

        if ($_productCollection->count()) {
            return true;
        }

        return false;
    }


I solved this by the below function. I Hope this may help some one.

function checkInWishilist($_product){
    Mage::getSingleton('customer/session')->isLoggedIn();
    $session = Mage::getSingleton('customer/session');
    $cidData = $session->isLoggedIn();
    $customer_id = $session->getId();

    if($customer_id){
        $wishlist = Mage::getModel('wishlist/item')->getCollection();
        $wishlist->getSelect()
                  ->join(array('t2' => 'wishlist'),
                         'main_table.wishlist_id = t2.wishlist_id',
                         array('wishlist_id','customer_id'))
                         ->where('main_table.product_id = '.$_product->getId().' AND t2.customer_id='.$customer_id);
        $count = $wishlist->count();
        $wishlist = Mage::getModel('wishlist/item')->getCollection();
    }
    else {
        $count="0";
    }

    if ($count) :
        return true;
    else:
        return false;
    endif;
}


Since @alexadr.parhomenk s' solution causes undesired results, here's a smaller method that does the trick:

/**
 * Check customers wishlist on identity product.
 * @param int $productId
 * @return bool
 */
public function isInWishlist($productId)
{
    $ids = Mage::registry('wishlist_ids');
    if (!$ids) {
        $productCollection = Mage::helper('wishlist')->getWishlistItemCollection();
        $ids = $productCollection->getColumnValues('product_id');
        Mage::register('wishlist_ids', $ids);
    }
    return in_array($productId, $ids);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消