Add All products in Cart to Wishlist

Want to add all the products in your cart to your Wishlist, in one click?
I’ll show you how to do it here ;) This procedure will take you 5 Minutes

We will begin to override the Wishlist Controller.
We do this by creating a folder called Mycompany in your webroot /app/code/local/ and inside it, we’ll add the folder: Addcwl

Inside this folder we will create two extra folders:

etc
controllers

Now, we are ready to begin:

In WEBROOT/app/code/local/Mycompany/Addcwl/etc/ create the file config.xml
Inside this file put the following,

<?xml version="1.0" encoding="utf-8" ?>
<config>
   <!-- snip -->
   <frontend>
      <routers>
         <wishlist>
            <args>
               <modules>
                  <Mycompany_Addcwl before="Mage_Wishlist">
                            Mycompany_Addcwl
                 </Mycompany_Addcwl>
               </modules>
            </args>
         </wishlist>
      </routers>
   </frontend>
</config>

The above code allows you to override the Wishlist controller :)

Now, in WEBROOT/app/code/local/Mycompany/Addcwl/controllers/ create the file

IndexController.php

Inside this file put the following:

<?php

 * @category   BUYX
 * @package
 * @copyright  Copyright (c)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Wishlist front controller
 *
 * @category   BUYX
 * @package
 * @author     Winston nolan <winston@buyxonline.com>
 */

include_once("Mage/Wishlist/controllers/IndexController.php");

class Mycompany_Addcwl_IndexController extends Mage_Wishlist_IndexController
{
	//we test our controller override with this
	/*
	public function indexAction()
	    {
	        # Just to make sure
	        error_log('Yes, I did it!');
	        parent::indexAction();
	    }
	*/

    /**
     * Adding new item
     */

    public function addcartAction()
    {
        $session = Mage::getSingleton('customer/session');
        $wishlist = $this->_getWishlist();
        if (!$wishlist) {
            $this->_redirect('*/');
            return;
        }
        // We store the id's of our products in the cart, in our core session
        $ids = Mage::getSingleton('core/session')->getWishListIds();
        // Here we explode our id's
        $ids = explode('-', $ids);
                        //do a foreach on them
        		foreach ($ids as $productId) {

        $product = Mage::getModel('catalog/product')->load($productId);

        try {

            $wishlist->addNewItem($productId);
            Mage::dispatchEvent('wishlist_addcart_product', array('wishlist'=>$wishlist, 'product'=>$product));

            //unset wishlist id's in session
	    Mage::getSingleton('core/session')->unsWishListIds();

            if ($referer = $session->getBeforeWishlistUrl()) {
                $session->setBeforeWishlistUrl(null);
            }
            else {
                $referer = $this->_getRefererUrl();
            }
            $message = $this->__('%1$s was successfully added to your wishlist. Click <a href="%2$s">here</a> to continue shopping', $product->getName(), $referer);
            $session->addSuccess($message);
        	}
        	catch (Mage_Core_Exception $e) {
            $session->addError($this->__('There was an error while adding item to wishlist: %s', $e->getMessage()));
        	}
        	catch (Exception $e) {
            //$session->addError($this->__('There was an error while adding item to wishlist.'));
        	}
        $this->_redirect('*');
    	}

 	}
}

Now that have the controller wrapped up, we need to modify one file called sidebar.phtml
The file is located here here:

WEBROOT/app/design/frontend/YOURPACKAGE/YOURTHEME/checkout/sidebar/sidebar.phtml

In this file we’ll make the following change around line 56

<?php //first we'll make our id's null ?>
<?php $ids = null; ?>
    <?php foreach($_items as $_item): ?>
        <?php echo $this->getItemHtml($_item) ?>
         <?php now we'll concatenate our Id's into a string
        <?php $ids .= '-' . $_item['product_id']; ?>
    <?php endforeach; ?>
    <?php
        //finally, we'll store the id's into our core session
    	Mage::getSingleton('core/session')->setWishListIds($ids);

         //setup some debug logging to see what we're doing :)
    	//$session = Mage::getSingleton('core/session');
    	//Mage::Helper('debug')->log($session);
     ?>

And we’ll add the Add All to Wishlist button/link here:

<!-- add all items to wishlist -->
        <button style="margin-right:5px;" class="form-button" type="button" onclick="setLocation('<?php echo Mage::getBaseUrl(); ?>wishlist/index/addcart/product/')">
            <span><?php echo $this->__('Add to my Wishlist') ?></span>
        </button>

And now for the Grand Finale!
We’ll activate our module by creating a file in

WEBROOT/app/etc/modules/Mycompany_Addcwl.xml
Inside this file we’ll put the following

<?xml version="1.0"?>
<config>
 <modules>
 <!-- Activate Add All in Cart to Wishlist Module -->
   <Mycompany_Addcwl>
     <active>true</active>
     <codePool>local</codePool>
   </Mycompany_Addcwl>
 </modules>
</config>

Congratulations, have a beer! You can now add all the products in your cart to your wishlist :)

Leave a Reply