I want to remove the wishlist products that are added to the cart. I've followed this link: Keeping wishlist items for keeping the wish list items after being added to the cart.i have set my $delete to true as shown in the code below but the product isn't removed from the wishlist.
Here is my indexcontroller.php path and code
app/code/core/Mage/Wishlist/controllers/IndexController.php
/**
* Add wishlist item to shopping cart and remove from wishlist
*
* If Product has required options - item removed from wishlist and redirect
* to product view page with message about needed defined required options
*/
public function cartAction()
{
if (!$this->_validateFormKey()) {
return $this->_redirect('*/*');
}
$itemId = (int) $this->getRequest()->getParam('item');
/* @var $item Mage_Wishlist_Model_Item */
$item = Mage::getModel('wishlist/item')->load($itemId);
if (!$item->getId()) {
return $this->_redirect('*/*');
}
$wishlist = $this->_getWishlist($item->getWishlistId());
if (!$wishlist) {
return $this->_redirect('*/*');
}
// Set qty
$qty = $this->getRequest()->getParam('qty');
if (is_array($qty)) {
if (isset($qty[$itemId])) {
$qty = $qty[$itemId];
} else {
$qty = 1;
}
}
$qty = $this->_processLocalizedQty($qty);
if ($qty) {
$item->setQty($qty);
}
/* @var $session Mage_Wishlist_Model_Session */
$session = Mage::getSingleton('wishlist/session');
$cart = Mage::getSingleton('checkout/cart');
$redirectUrl = Mage::getUrl('*/*');
try {
$options = Mage::getModel('wishlist/item_option')->getCollection()
->addItemFilter(array($itemId));
$item->setOptions($options->getOptionsByItem($itemId));
$buyRequest = Mage::helper('catalog/product')->addParamsToBuyRequest(
$this->getRequest()->getParams(),
array('current_config' => $item->getBuyRequest())
);
$item->mergeBuyRequest($buyRequest);
if ($item->addToCart($cart, true)) {
$cart->save()->getQuote()->collectTotals();
}
$wishlist->save();
Mage::helper('wishlist')->calculate();
if (Mage::helper('checkout/cart')->getShouldRedirectToCart()) {
$redirectUrl = Mage::helper('checkout/cart')->getCartUrl();
}
Mage::helper('wishlist')->calculate();
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($item->getProductId());
$productName = Mage::helper('core')->escapeHtml($product->getName());
$message = $this->__('%s was added to your shopping cart.', $productName);
Mage::getSingleton('catalog/session')->addSuccess($message);
} catch (Mage_Core_Exception $e) {
if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_NOT_SALABLE) {
$session->addError($this->__('This product(s) is currently out of stock'));
} else if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS) {
Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
$redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
} else {
Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
$redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
}
} catch (Exception $e) {
Mage::logException($e);
$session->addException($e, $this->__('Cannot add item to shopping cart'));
}
Mage::helper('wishlist')->calculate();
return $this->_redirectUrl($redirectUrl);
}
Here is my Item.php path and code
app/code/core/Mage/Wishlist/Model/Item.php
/**
* Add or Move item product to shopping cart
*
* Return true if product was successful added or exception with code
* Return false for disabled or unvisible products
*
* @throws Mage_Core_Exception
* @param Mage_Checkout_Model_Cart $cart
* @param bool $delete delete the item after successful add to cart
* @return bool
*/
public function addToCart(Mage_Checkout_Model_Cart $cart, $delete=true)
{
$product = $this->getProduct();
$storeId = $this->getStoreId();
if ($product->getStatus() != Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
return false;
}
if (!$product->isVisibleInSiteVisibility()) {
if ($product->getStoreId() == $storeId) {
return false;
}
}
if (!$product->isSalable()) {
throw new Mage_Core_Exception(null, self::EXCEPTION_CODE_NOT_SALABLE);
}
$buyRequest = $this->getBuyRequest();
$cart->addProduct($product, $buyRequest);
if (!$product->isVisibleInSiteVisibility()) {
$cart->getQuote()->getItemByProduct($product)->setStoreId($storeId);
}
if ($delete) {
$this->delete();
}
return true;
}