Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2: Override/Rewrite Wishlist controller

In seeking how to override a controller in Magento 2, you find lots of different ways to proceed. Some work, some do not. I will show you how to override a controller very easily. I am going to override wishlist controller. Lets see step by step how to do.

Magento 2: Override/Rewrite Wishlist controller


Step1: Create directory structure

app/code/HK/Localwishlist/Controller/Wishlist/Add.php
app/code/HK/Localwishlist/etc/di.xml
app/code/HK/Localwishlist/etc/module.xml
app/code/HK/Localwishlist/registration.php

Step2: Add this code in your Add.php file

<?php
namespace HK\Localwishlist\Controller\Wishlist;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Action;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Controller\ResultFactory;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Add extends \Magento\Wishlist\Controller\AbstractIndex
{
    /**
     * @var \Magento\Wishlist\Controller\WishlistProviderInterface
     */
    protected $wishlistProvider;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @param Action\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
        Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
        ProductRepositoryInterface $productRepository
    ) {
        $this->_customerSession = $customerSession;
        $this->wishlistProvider = $wishlistProvider;
        parent::__construct($context);
        $this->productRepository = $productRepository;
    }

    public function execute(){
            
        echo "Test.....";
        
    }
}


Step3: Add this code in your di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
        <preference for="Magento\Wishlist\Controller\Index\Add" type="HK\Localwishlist\Controller\Wishlist\Add" />
</config>

Step4: Add this code in your module.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
 <module name="HK_Localwishlist" setup_version="1.0.0"/>
</config>

Step5: Add this code in your registration.php file

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'HK_Localwishlist',
    __DIR__
);

Now use this command,
php bin/magento setup:upgrade

Download

Please support us, Like us on Facebook.

  1. Hello friends, nice piece of writing and good arguments commented
    here, I am in fact enjoying by these.

    ReplyDelete

 

Copyright @ 2017 HKBlog.