Question

I'm trying to create a link to remove an item from the wishlist if it's there. The URL is returning /wishlist/index/remove instead of the correct URL which from what I've seen online should be /wishlist/index/remove/item/[ID]. Trying that URL manually doesn't work. This is my way of getting the remove URL:

$_wishlistRemoveUrl = $this->helper('wishlist')->getRemoveUrl($_product);
Was it helpful?

Solution

First, an overview of how wishlists work:

  1. Each wishlist is stored in the database with its own ID
  2. Each product added to a wishlist is assigned a wishlist_item_id
  3. This ID is not the same as the product_id

For this reason you can't just pass in a product ID to the remove URL because it's actually referencing the product's wishlist_item_id.

This explains why you are getting a blank url ("/wishlist/index/remove"), because when you pass in the product it doesn't have the required wishlist_item_id, you have to retrieve this in a different way.

To get around this, there is a method in Mage_Wishlist_Model_Item you can use to get a wishlist item by product ID:

    /** @var $_wishlistHelper Mage_Wishlist_Helper_Data */
    $_wishlistHelper = $this->helper('wishlist');

    /** @var $_wishlist Mage_Wishlist_Model_Wishlist */
    $_wishlist = $_wishlistHelper->getWishlist();

    /** @var $_wishlistItem Mage_Wishlist_Model_Item */
    $_wishlistItem = Mage::getModel('wishlist/item');
    $_wishlistItem->loadByProductWishlist(
        $_wishlist->getId(),
        $_product->getId(),
        $this->helper('core')->getStoreId()
    );

    $_wishlistRemoveUrl = $_wishlistHelper->getRemoveUrl($_wishlistItem);

Or the shorter way:

    $_wishlistItem = Mage::getModel('wishlist/item')->loadByProductWishlist(
        $this->helper('wishlist')->getWishlist()->getId(),
        $_product->getId(),
        $_product->getStoreId()
    );
    $_wishlistRemoveUrl = $this->helper('wishlist')->getRemoveUrl($_wishlistItem);

This code has been tested in blocks of type Mage_Catalog_Block_Product_List and Mage_Catalog_Block_Product_View

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top