0

I'm trying to add product to wishlist without redirecting in to wishlist page i need to stay it on same page here is my code that how i tried

app/code/local/Wish/Mwishlist/etc

<?xml version="1.0"?>
<config>
    <modules>
        <Wish_Mwishlist>
            <version>0.1.0</version>
        </Wish_Mwishlist>
    </modules>

    <frontend>
        <routers>
            <wishlist>
                <args>
                    <modules>
                        <Wish_Mwishlist before="Mage_Wishlist">Wish_Mwishlist</Wish_Mwishlist>
                    </modules>
                </args>
            </wishlist>
        </routers>
    </frontend>
</config>

app/code/local/Wish/Mwishlist/controllers/Wishlist/IndexController.php

  <?php
require_once Mage::getModuleDir('controllers', 'Mage_Wishlist') . DS . 'IndexController.php';
class Wish_Mwishlist_IndexController extends Mage_Wishlist_IndexController
{
/**
 * Add the item to wish list
 *
 * @return Mage_Core_Controller_Varien_Action|void
 */
protected function _addItemToWishList()
{
    Mage::log("control get",null,"wish.log");
    $wishlist = $this->_getWishlist();
    if (!$wishlist) {
        return $this->norouteAction();
    }

    $session = Mage::getSingleton('customer/session');

    $productId = (int)$this->getRequest()->getParam('product');
    if (!$productId) {
        $this->_redirect('*/');
        return;
    }

    $product = Mage::getModel('catalog/product')->load($productId);
    if (!$product->getId() || !$product->isVisibleInCatalog()) {
        $session->addError($this->__('Cannot specify product.'));
        $this->_redirect('*/');
        return;
    }

    try {
        $requestParams = $this->getRequest()->getParams();
        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.'));
    }

    //$this->_redirect('*', array('wishlist_id' => $wishlist->getId()));
    $this->_redirectReferer();
}

}

But this code not working for me can anybody help me to do this?

Here is the code how i call add to wishlist url in catalog page

<a  href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" title="Add to Wishlist"><i class="fa fa-heart-o" aria-hidden="true"></i></a></li>
0

Magento,your issue in config.xml

There are some space in config.xml

<Wish_Mwishlist before="Mage_Wishlist">
                            Wish_Mwishlist
                        </Wish_Mwishlist>

need to change it to

<Wish_Mwishlist before="Mage_Wishlist">Wish_Mwishlist</Wish_Mwishlist>
  • i have updated my code please have a look on that i have also added how i call the wishlist url still it redirecting in to wishlist page .i think the controller is not working there i added a code to create log file its not working – amith lal Mar 30 '17 at 5:32
0

A lot of the requirements you need for that are already in the controller. There is this piece of code that you need to hook into:

$referer = $session->getBeforeWishlistUrl();
if ($referer) {
    $session->setBeforeWishlistUrl(null);
} else {
    $referer = $this->_getRefererUrl();
}

So all you need to do is to make sure that before_wishlist_url is set and that you redirect to that page.

Just before the code above, there is an event being dispatched. So you can set the value there by hooking into the event wishlist_add_product with a custom observer.

After that, you can change the last line of your code to something like

$this->_redirect($referer);

This should be enough to get what you want.

  • still same issue the controller not working i think its loading from core file is there anything mistake in my local file – amith lal Mar 30 '17 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.