I have written an extension support save all items from cart to wishlist in magento.
This extension help customers easy to save all the cart items to their wishlist for later order.
It supports all kind of products in magento : Simple product, configurable product, bundle product , simple product with custom option…
It will compatible with magento community : 1.7.x.x,1.8.x.x
Images for demo extension are listed as below :
In this tutorial, I will show you how to make it done. Here is how :
1/ Create a xml file Inbusiness_Wishlist.xml to declare your module . It is located in : app/etc/modules/Inbusiness_Wishlist.xml
<?xml version="1.0"?> <config> <modules> <Inbusiness_Wishlist> <active>true</active> <codePool>local</codePool> </Inbusiness_Wishlist> </modules> </config>
2/ Create app/code/local/Inbusiness/Wishlist/etc/config.xml as the below code :
<?xml version="1.0"?> <config> <modules> <Inbusiness_Wishlist> <version>1.1.1</version> </Inbusiness_Wishlist> </modules> <frontend> <routers> <checkout> <use>standard</use> <args> <modules> <Inbusiness_Wishlist before="Mage_Checkout">Inbusiness_Wishlist_Checkout</Inbusiness_Wishlist> </modules> </args> </checkout> </routers> </frontend> </config>
3/ Create Inbusiness_Wishlist_Checkout_CartController which is extended from Mage_Checkout_CartController to handle save all items from cart to wishlist. By default,Mage_Checkout_CartController only have empty_cart, update_qty and default action. So , I added more option : update_wislist.
<?php require_once 'Mage/Checkout/controllers/CartController.php'; class Inbusiness_Wishlist_Checkout_CartController extends Mage_Checkout_CartController { public function updatePostAction(){ if (!$this->_validateFormKey()) { $this->_redirect('*/*/'); return; } $updateAction = (string)$this->getRequest()->getParam('update_cart_action'); switch ($updateAction) { case 'empty_cart': $this->_emptyShoppingCart(); break; case 'update_qty': $this->_updateShoppingCart(); break; case 'update_wishlist': $this->_updateItemsToWishlist(); break; default: $this->_updateShoppingCart(); } $this->_goBack(); } protected function getWishlistInb(){ $wishlist = Mage::registry('wishlist'); if ($wishlist) { return $wishlist; } try { if (!$wishlistId) { $wishlistId = $this->getRequest()->getParam('wishlist_id'); } $customerId = Mage::getSingleton('customer/session')->getCustomerId(); /* @var Mage_Wishlist_Model_Wishlist $wishlist */ $wishlist = Mage::getModel('wishlist/wishlist'); if ($wishlistId) { $wishlist->load($wishlistId); } else { $wishlist->loadByCustomer($customerId, true); } if (!$wishlist->getId() || $wishlist->getCustomerId() != $customerId) { $wishlist = null; Mage::throwException( Mage::helper('wishlist')->__("Requested wishlist doesn't exist") ); } Mage::register('wishlist', $wishlist); } catch (Mage_Core_Exception $e) { Mage::getSingleton('wishlist/session')->addError($e->getMessage()); return false; } catch (Exception $e) { Mage::getSingleton('wishlist/session')->addException($e, Mage::helper('wishlist')->__('Wishlist could not be created.') ); return false; } return $wishlist; } protected function _updateItemsToWishlist(){ try { //$wishlist = Mage::helper('wishlist')->getWishlist(); $wishlist = $this->getWishlistInb(); if (!$wishlist) { $this->_goBack(); } $session = $this->_getSession(); $cart = $this->_getCart()->getQuote(); //getAllItems foreach ($cart->getAllVisibleItems() as $item) { $product = $item->getProduct(); $productId = $item->getProductId(); $buyRequest = $item->getBuyRequest(); try { $result = $wishlist->addNewItem($productId, $buyRequest); if (is_string($result)) { Mage::throwException($result); } $productName = Mage::helper('core')->escapeHtml($item->getProduct()->getName()); $wishlistName = Mage::helper('core')->escapeHtml($wishlist->getName()); $message = Mage::helper('wishlist')->__("%s has been added to your wishlist %s", $productName, $wishlistName); $session->addSuccess($message); $wishlist->save(); } 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.')); } } } catch (Mage_Core_Exception $e) { $this->_getSession()->addError(Mage::helper('core')->escapeHtml($e->getMessage())); } catch (Exception $e) { $this->_getSession()->addException($e, $this->__('Cannot update items to wishlist.')); Mage::logException($e); } } }
4/ Adding new button “Update all items to wishlist” below “Clear Shopping Cart” button in app/design/frontend/your_current_packagetheme/your_current_theme/template/checkout/cart.phtml
<button type="submit" name="update_cart_action" value="update_qty" title="<?php echo $this->__('Update Shopping Cart'); ?>" class="button btn-update"><span><span><?php echo $this->__('Update Shopping Cart'); ?></span></span></button> <button type="submit" name="update_cart_action" value="empty_cart" title="<?php echo $this->__('Clear Shopping Cart'); ?>" class="button btn-empty" id="empty_cart_button"><span><span><?php echo $this->__('Clear Shopping Cart'); ?></span></span></button> <?php if($this->helper('customer')->isLoggedIn()) : ?> <button type="submit" name="update_cart_action" value="update_wishlist" title="<?php echo $this->__('Update All Items to Wishlist'); ?>" class="button btn-update" style="margin-right:10px"><span><span><?php echo $this->__('Update All Items to Wishlist'); ?></span></span></button> <?php endif; ?>
Happy coding. Please leave your comment if you have any troubles with it. Feel free to contact with me.
Liz Yokum
April 19
This is exactly what I am trying to do! However it does not seem to work. When clicking on the “update all to wishlist,” button nothing happens. Perhaps it is my own lack of knowledge as to where to save the controller? I saved the controller code you included in app/code/local/Inbusiness/Wishlist/controllers/IndexController.php.
thienphucvx
April 19
Thanks for your comments. Glad to help you ! 😀
Liz Yokum
April 19
I have found what may be a bug.. when a new user attempts to “update all to wishlist” for the first time, it throws an error. I am assuming this is because the wishlist doesn’t exist yet. Once an item has been added via other means, the update all button works. It is just if it is clicked before anything has ever been added to a new customer list.
thienphucvx
April 19
Just remove this code in function _updateItemsToWishlist():
if (is_string($result)) {Mage::throwException($result);}
It will works as normal.
Liz Yokum
April 19
I tried commenting it out yet I am still running into the same issue. I will update if I figure out the problem. Thanks!
Here is the error: Cannot specify wishlist
Liz Yokum
April 19
It seems that this is the function failing: if (!$this->getWishlistId()) in Mage_Wishlist_Model_Item (/app/code/core/Mage/Wishlist/Model/Item.php)
Liz Yokum
April 19
So strange, I have been working on this for hours and can’t figure out why this is failing. When it’s the first time a user clicks the button to add a cart to the wishlist, $wishlist is returned as empty, so there is no getID(). Once an item is added to the wishlist via other means, the wishlist is created and then the mage call to create variable $wishlist works just fine. Is this a core problem?
thienphucvx
April 19
Sorry for answer late. You’re right. It is a bug of (!$this->getWishlistId()). And it come from the other bug of function Mage::helper(‘wishlist’)->getWishlist() : It doesn’t generate new wishlist for new customer. So I create other function getWishlistInb() to resolve this problem. Please look at new code in this tutorial.
Liz Yokum
April 19
This is fantastic, thanks so much for your help!
thienphucvx
April 19
You’re welcome. If you have any trouble, feel free to contact with me !.
pushpendra
April 19
sir when i clicking on update all items to wishlist..nothing happens..can u tell me what may be the problem
pushpendra
April 19
can u tell me where is the controller should be saved?
thienphucvx
April 19
Hi,
The CartController.php is located in app/code/local/Inbusiness/Wishlist/controllers/Checkout folder
pushpendra
April 19
thanks 4 reply sir. now when i am click on add all items in to wishlist it is not working..can u tell me the problem
pushpendra
April 19
Dear Sir,
Thanks 4 reply..but when i clicking on update all items
into wishlist then wishlist is not updating . please solve my problem..or can u tell how to move all items in to wishlist . i need this.
thienphucvx
April 19
Hi,
Please check all files and codes according to the above tutorial. I already checked it before writing it. If you follow these steps, “save all items from cart to wishlist” should work fine. You also should notice the step 4, the “Update all items to wishlis” should be located in your current theme and only be displayed when customer logged in.