This is tutorial for guiding create a button adding multiple products to wishlist in category page. Let’s start !
Firstly, We need to create a button. Go to the the file : app/design/frontend/default/your-current-theme/catalog/product/list.phtml
Put this code . Notice my highlighter code
<?php /** * Product list template * * @see Mage_Catalog_Block_Product_List */ ?> <?php $_productCollection=$this->getLoadedProductCollection(); $_helper = $this->helper('catalog/output'); ?> <?php if(!$_productCollection->count()): ?> <p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p> <?php else: ?> <div class="category-products"> <form action="<?php echo Mage::getUrl('wishlist/index/addmultiple') ;?>" method="post" id="products_addtowishlist_form" enctype="multipart/form-data"> <input type="hidden" name="wishlist_products" id="wishlist_products" value="" /> <button class="form-button button btn-cart" onclick="this.form.submit()"><span><span><?php echo $this->__('Put all in wishlist') ?></span></span></button> </form> <?php echo $this->getToolbarHtml() ?> <?php // List mode ?> <?php if($this->getMode()!='grid'): ?> <?php $_iterator = 0; ?> <ol class="products-list" id="products-list"> <?php foreach ($_productCollection as $_product): ?> <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>"> <?php // Product Image ?> <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a> <?php // Product description ?> <div class="product-shop"> <div class="f-fix"> <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?> <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2> <?php if($_product->getRatingSummary()): ?> <?php echo $this->getReviewsSummaryHtml($_product) ?> <?php endif; ?> <?php echo $this->getPriceHtml($_product, true) ?> <?php if($_product->isSaleable()): ?> <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p> <input type="hidden" name="products[]" value="<?php echo $_product->getId() ?>" /> <?php else: ?> <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p> <?php endif; ?> <div class="desc std"> <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?> <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a> </div> <ul class="add-to-links"> <?php if ($this->helper('wishlist')->isAllow()) : ?> <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li> <?php endif; ?> <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?> <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li> <?php endif; ?> </ul> </div> </div> </li> <?php endforeach; ?> </ol> <script type="text/javascript">decorateList('products-list', 'none-recursive')</script> <?php else: ?> <?php // Grid Mode ?> <?php $_collectionSize = $_productCollection->count() ?> <?php $_columnCount = $this->getColumnCount(); ?> <?php $i=0; foreach ($_productCollection as $_product): ?> <?php if ($i++%$_columnCount==0): ?> <ul class="products-grid"> <?php endif ?> <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>"> <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a> <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2> <?php if($_product->getRatingSummary()): ?> <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?> <?php endif; ?> <?php echo $this->getPriceHtml($_product, true) ?> <div class="actions"> <?php if($_product->isSaleable()): ?> <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button> <input type="hidden" name="products[]" value="<?php echo $_product->getId() ?>" /> <?php else: ?> <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p> <?php endif; ?> <ul class="add-to-links"> <?php if ($this->helper('wishlist')->isAllow()) : ?> <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li> <?php endif; ?> <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?> <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li> <?php endif; ?> </ul> </div> </li> <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?> </ul> <?php endif ?> <?php endforeach ?> <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script> <?php endif; ?> <div class="toolbar-bottom"> <?php echo $this->getToolbarHtml() ?> </div> </div> <?php endif; ?> <script type="text/javascript"> jQuery('#products_addtowishlist_form').on('submit',function(){ var products = {}; var i = 0; jQuery('input[name="products[]"]').each(function() { var productId = jQuery(this).val(); products[i]= productId; i++; }); jQuery('#wishlist_products').val(JSON.stringify(products)); return; }); </script>
[/code]
Secondly, We also need a controller to handle this action. I recommend you to create a new controller which is extended Mage_Wishlist_IndexController .
In this case, to easy management we create a new module , so we can disable or custom it later.
Create app/etc/modules/Yourcompany_Wishlist.xml for registering new module in magento :
<?xml version="1.0"?> <config> <modules> <Yourcompany_Wishlist> <active>true</active> <codePool>local</codePool> </Yourcompany_Wishlist> </modules> </config>
Create app/code/local/Yourcompany/Wishlist/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Yourcompany_Wishlist> <version>1.0.0</version> </Yourcompany_Wishlist> </modules> <frontend> <routers> <wishlist> <use>standard</use> <args> <modules> <Yourcompany_Wishlist before="Mage_Wishlist">Yourcompany_Wishlist</Yourcompany_Wishlist> </modules> </args> </wishlist> </routers> </frontend> </config>
Create a controller to handle request adding multiple products to wishlist
<?php require_once 'Mage/Wishlist/controllers/IndexController.php'; class Yourcompany_Wishlist_IndexController extends Mage_Wishlist_IndexController { public function addmultipleAction(){ $wishlist = $this->_getWishlist(); if (!$wishlist) { return $this->norouteAction(); } $session = Mage::getSingleton('customer/session'); $products = $this->getRequest()->getParam('wishlist_products'); $products = json_decode($products,true); if(!is_array($products)){ $this->_redirect('*/'); return ; } foreach($products as $productId): $productId = (int)$productId; $product = Mage::getModel('catalog/product')->load($productId); if (!$product->getId() || !$product->isVisibleInCatalog()) continue; try { $requestParams = $products; if ($session->getBeforeWishlistRequest()) { $requestParams = $session->getBeforeWishlistRequest(); $session->unsBeforeWishlistRequest(); } $buyRequest = new Varien_Object($requestParams); $result = $wishlist->addNewItem($product, $buyRequest); if (is_string($result)) { Mage::throwException($result); } $wishlist->save(); Mage::dispatchEvent( 'wishlist_add_product', array( 'wishlist' => $wishlist, 'product' => $product, 'item' => $result ) ); $referer = $session->getBeforeWishlistUrl(); if ($referer) { $session->setBeforeWishlistUrl(null); } else { $referer = $this->_getRefererUrl(); } /** * Set referer to avoid referring to the compare popup window */ $session->setAddActionReferer($referer); Mage::helper('wishlist')->calculate(); $message = $this->__('%1$s has been added to your wishlist. Click <a href="%2$s">here</a> to continue shopping.', $product->getName(), Mage::helper('core')->escapeUrl($referer)); $session->addSuccess($message); } 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.')); } endforeach; $this->_redirect('*', array('wishlist_id' => $wishlist->getId())); } } ?>
Try to refresh your cache to see this changes. That’s finish. Happy coding!
Geert van der Heide
April 4
Thanks a lot for this code! I used your controller and some customization to create an “Add all to wishlist” link on the cart page. Saved me a lot of headache. Hope your post reaches a higher Google position though, I was about to give up when I found your post on page 3 of the search results 😀
thienphucvx
April 4
Hi
Thanks for your feedback. It inspires me a lot. I has just started writing blog magento. Hope it will display top as google search results soon!