0

Is there a way to restrict the quantity in the wishlist to 1 similar to how you can for the cart? I will only ever have one of any item however if the customer clicks add to wishlist multiple times on the item it increases the quantity for that item.

Thanks in advance.

0

1 Answer 1

1

You have to override magento wishlist IndexController.

for override wishlist in IndexController refer Question

Just put below function in it.

Use _addItemToWishList()

public function _addItemToWishList()
{
    $wishlist = $this->_getWishlist();
    if (!$wishlist) {
        return $this->norouteAction();
    }

    $session = Mage::getSingleton('customer/session');

    $productId = (int)$this->getRequest()->getParam('product');
    if (!$productId) {
        $this->_redirect('*/');
        return;
    }

    $product = Mage::getModel('catalog/product')->load($productId);
    if (!$product->getId() || !$product->isVisibleInCatalog()) {
        $session->addError($this->__('Cannot specify product.'));
        $this->_redirect('*/');
        return;
    }

    //*******************Check if already exist****************

    $customer = $session->getCustomer();
    $already_exist=false;
    if($customer->getId())
    {
        $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
        $wishListItemCollection = $wishlist->getItemCollection();
        foreach ($wishListItemCollection as $item)
        {
            if($item->getId()==$productId)
            {
                $already_exist=true;
                break;
            }
        }
    }
    if($already_exist)
    {
        $session->addError($this->__('%s already exist in wishlist: %s', $product->getName()));
        $this->_redirect('*/');
        return;
    }

    //******************************************************

    try {
        $requestParams = $this->getRequest()->getParams();
        if ($session->getBeforeWishlistRequest()) {
            $requestParams = $session->getBeforeWishlistRequest();
            $session->unsBeforeWishlistRequest();
        }
        $buyRequest = new Varien_Object($requestParams);

        $result = $wishlist->addNewItem($product, $buyRequest);
        if (is_string($result)) {
            Mage::throwException($result);
        }
        $wishlist->save();

        Mage::dispatchEvent(
            'wishlist_add_product',
            array(
                'wishlist' => $wishlist,
                'product' => $product,
                'item' => $result
            )
        );

        $referer = $session->getBeforeWishlistUrl();
        if ($referer) {
            $session->setBeforeWishlistUrl(null);
        } else {
            $referer = $this->_getRefererUrl();
        }

        /**
         *  Set referer to avoid referring to the compare popup window
         */
        $session->setAddActionReferer($referer);

        Mage::helper('wishlist')->calculate();

        $message = $this->__('%1$s has been added to your wishlist. Click <a href="%2$s">here</a> to continue shopping.',
            $product->getName(), Mage::helper('core')->escapeUrl($referer));
        $session->addSuccess($message);
    } catch (Mage_Core_Exception $e) {
        $session->addError($this->__('An error occurred while adding item to wishlist: %s', $e->getMessage()));
    }
    catch (Exception $e) {
        $session->addError($this->__('An error occurred while adding item to wishlist.'));
    }

    $this->_redirect('*', array('wishlist_id' => $wishlist->getId()));
}
2
  • works perfectly, thank you for your help. As this controller is not in my template and I do not want to modify the core files can you give some advice on how to move this to my template. Do I simply copy the whole wishlist folder from the core directly into my template directory?
    – Mage17
    Commented Oct 21, 2016 at 20:54
  • @Mage17 answer updated for override wishlist controller. Commented Oct 22, 2016 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.