Pregunta

Estoy intentando crear un enlace para eliminar un elemento de la lista si está allí. La URL está regresando / deseos / index / remove en lugar de la URL correcta que a partir de lo que he visto en línea debe ser / deseos / index / remove / artículo / [ID]. Tratando de que la URL de forma manual no funciona. Esta es mi manera de conseguir la URL de eliminación:

$_wishlistRemoveUrl = $this->helper('wishlist')->getRemoveUrl($_product);
¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top