检查产品是否在愿望清单中

发布于 2024-11-25 02:10:35 字数 207 浏览 0 评论 0原文

我正在开发 Magento 主题,我需要构建一个函数来检查产品是否已添加到用户的愿望清单中。

Magento 有一个“Mage_Wishlist_Helper_Data”帮助程序类,但我不知道如何构建一个检查是否已在愿望列表中的函数。基本上我需要使用 Magento 的愿望清单功能来构建收藏夹列表。如果特定产品已添加到用户的收藏夹中,我想向“添加到愿望清单”链接添加一个特殊的类。

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" helper 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迟月 2024-12-02 02:10:35
 <?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>";
  ;?>
 <?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>";
  ;?>
千纸鹤带着心事 2024-12-02 02:10:35

由于集合是延迟加载的,我假设您可以执行以下操作:

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

您可以对其他字段执行类似的过滤,但在这种情况下 SKU 应该足够了。

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.

吃素的狼 2024-12-02 02:10:35

我现在才回到这个项目,但我采纳了 Daniel Sloof 的建议,并且使用下面的函数完美地工作了:

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

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

这会检查产品是否已添加到当前用户的愿望清单中。我将其称为我的模板文件,如下所示:

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

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)) :
黯淡〆 2024-12-02 02:10:35

我在检查 Mage::helper('wishlist')->getWishlistItemCollection() 的选择查询后找到了这个解决方案。我希望这个解决方案对某人有所帮助。

   /**
     * 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 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;
    }
淡淡的优雅 2024-12-02 02:10:35

我通过下面的函数解决了这个问题。我希望这可以帮助某人。

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;
}

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;
}
岁月静好 2024-12-02 02:10:35

由于 @alexadr.parhomenk 的解决方案会导致不良结果,因此这里有一个较小的方法可以解决这个问题:

/**
 * 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文