Tag: magento wishlist

Keep product in the wishlist after adding to cart in magento

In Magento, after adding product from wishlist, they are completely remove from wishlist. If you need to keep this to wishlist after adding product and if it is fully control from admin, Then you need to code something. Today I will discuss about this topic that is depend on configuration so any time you can change it from admin.

Suppose NameSpace : MyPackage and Module : MyModule

Now create a module configuration file,name is MyPackage_MyModule.xml and location is

app/etc/modules/MyPackage_MyModule.xml
<?xml version="1.0"?>
<config>
    <modules>
        <MyPackage_MyModule>
            <active>true</active>
            <codePool>local</codePool>
        </MyPackage_MyModule>
    </modules>
</config>

Create configuration file

app/code/local/MyPackage/MyModule/etc/config.xml
<?xml version="1.0"?>
<config>
    <modules>
        <MyPackage_MyModule>
            <version>0.0.0.1</version>
        </MyPackage_MyModule>
    </modules>
    <global>
        <helpers>
            <mymodule>
                <class>MyPackage_MyModule_Helper</class>
            </mymodule>
        </helpers>
    </global>
    <frontend>
        <routers>
            <wishlist>
                <args>
                    <modules>
                        <mypackage_mymodule before="Mage_Wishlist">MyPackage_MyModule</mypackage_mymodule>
                    </modules>
                </args>
            </wishlist>
        </routers>
    </frontend>
</config>

Create System Configuration file

app/code/local/MyPackage/MyModule/etc/system.xml
<config>
    <tabs>
        <mymodule translate="label" module="mymodule">
            <label>My Module</label>
            <sort_order>100</sort_order>
        </mymodule>
    </tabs>
    <sections>
        <mypackage_mymodule>
            <label>My Module Configuration</label>
            <sort_order>1</sort_order>
            <tab>mymodule</tab>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <configuration translate="label">
                    <label>Keep products in the wishlist after adding them to cart configuration</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>2</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <keep_item translate="label">
                            <label>Keep item after adding them to cart</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </keep_item>
                    </fields>
                </configuration>
            </groups>
        </mypackage_mymodule>
    </sections>
</config>

Create ACL file

app/code/local/MyPackage/MyModule/etc/adminhtml.xml
<config>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <mypackage_mymodule translate="title" module="mymodule">
                                        <title>MyModule Section</title>
                                    </mypackage_mymodule>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</config>

Create helper file

app/code/local/MyPackage/MyModule/Helper/Data.php
<?php
class MyPackage_MyModule_Helper_Data extends Mage_Core_Helper_Abstract
{

}

Now Create Controller file

app/code/local/MyPackage/MyModule/controllers/IndexController.php
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Wishlist').DS.'IndexController.php';
class MyPackage_MyModule_IndexController extends Mage_Wishlist_IndexController
{
    /**
     * 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);
            $keepItem = Mage::getStoreConfig('mypackage_mymodule/configuration/keep_item');
            $keepItem = $keepItem?false:true;
            if ($item->addToCart($cart, $keepItem)) {
                $cart->save()->getQuote()->collectTotals();
            }

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

            if (Mage::helper('checkout/cart')->getShouldRedirectToCart()) {
                $redirectUrl = Mage::helper('checkout/cart')->getCartUrl();
            } else if ($this->_getRefererUrl()) {
                $redirectUrl = $this->_getRefererUrl();
            }
            Mage::helper('wishlist')->calculate();
        } 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);
    }
}

Clear cache and configure from System -> Configuration -> My Module Configuration

Happy Coding!