0

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

1 Answer 1

0

The addToCart() method is declared in app/code/core/Mage/Wishlist/Model/Item.php and the second parameter is what triggers the wishlist deletion. If you set it to false it won't delete the item from the wishlist:

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=fasle)
{
    $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;
}
1
  • Thank you for your concern and answer. Yes after searching magento SE i've realised that the boolean $delete when set to false it doesn't delete the item from the wishlist and when its set to true as shown in my code it must delete the item from the wishlist but its not removed! Any idea where can i be going wrong?
    – Ahmed wael
    Commented Dec 2, 2018 at 22:13

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.