Overwrite a function from an existing controller

Hello everybody,

We managed to overwrite a function from an existing controller into a plugin. For example we disabled the search by redirect user to another page:

We added in services.xml:

And in /src/Storefront/Controller/CustomSearchController.php:

namespace Plugin\MyCustomPlugin\Storefront\Controller;

use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\SearchController;
use Shopware\Storefront\Page\Search\SearchPageLoader;
use Shopware\Storefront\Page\Suggest\SuggestPageLoader;
class CustomSearchController extends SearchController
{
    /**
     * @var SearchPageLoader
     */
    private $searchPageLoader;

    /**
     * @var SuggestPageLoader
     */
    private $suggestPageLoader;

    /**
     * CustomSearchController constructor.
     * @param SearchPageLoader $searchPageLoader
     * @param SuggestPageLoader $suggestPageLoader
     */
    public function __construct(SearchPageLoader $searchPageLoader, SuggestPageLoader $suggestPageLoader)
    {
        $this->searchPageLoader = $searchPageLoader;
        $this->suggestPageLoader = $suggestPageLoader;
    }

    /**
     * Override search function
     * Do not allow search functionality
     * Redirect to home page
     *
     * @Route("/search", name="frontend.search.page", options={"seo"=false}, methods={"GET"})
     */
    public function search(SalesChannelContext $context, Request $request): Response
    {
        return $this->redirectToRoute('frontend.home.page');
    }

}

This is working, but when we try to overwrite the function ‚addLineItems‘ from CartLineItemController in the same manner as above nothing happens. My custom cart line item controller is ignored.

Did someone have an ideea how we can achieve this?

Thank you in advance!

Best regards,

Sorin

 

Hello,

Seems it was a mistake from my side.

I forgott to add a routes.xml file in config directory.

Best regards,

Sorin

2 „Gefällt mir“

Hi Sorin, how did you make it work?