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
1 2 3 4 5 6 7 8 9 |
<?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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
<?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
1 2 3 4 5 6 |
<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.