Moving from magento 1.9 to 2 -

looking for a way to do some of the things I had done in 1.9, one of those being, that if there's only one product in a category, just go to the product detail page when clicking that category....

just like this, but is there a way to do it in magento 2? :) thanks in advance!



after the response below, I have module in place, but cannot enable said module, how else can it be enabled besides shell access? I Have tried for hours now, and no success:

enter image description here


It's a bit more complex task in comparison with Magento 1. First of all you need to create a small custom extension and modify the block's method behaviour for getting products collection. Magento 2 plugins feature is very handy for this purpose.

create etc/di.xml in your extension with the following contents:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Block\Product\ListProduct">
    <plugin name="firstproduct-category-mod" type="Vendor\FirstProduct\Block\Catalog\Product\ListProduct" sortOrder="1"/>

then create the plugin class Block/Catalog/Product/ListProduct.php with the code below:


namespace Vendor\FirstProduct\Block\Catalog\Product;

class ListProduct
     * @var \Magento\Framework\App\Response\Http
    protected $response;

    public function __construct(
       \Magento\Framework\App\Response\Http $response
        $this->response = $response;

     * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $resultCollection
     * @param \Magento\Catalog\Block\Product\ListProduct $subject
     * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection $resultCollection
    public function afterGetLoadedProductCollection(\Magento\Catalog\Block\Product\ListProduct $subject, $resultCollection)
        if ($resultCollection->count() == 1) {
            /** @var \Magento\Catalog\Model\Product $product */
            $product = $resultCollection->getFirstItem();

        return $resultCollection;

That's it, we have used DI for Response object injection that allows us to use redirect in case if there's only 1 product in the products collection.

P.S. Do not forget to flush your cache storage after the extension implementation.

  • 1
    Although not a requirement, convention would dictate plugins should live in [Vendor]/[ModuleName]/Plugin devdocs.magento.com/guides/v2.0/extension-dev-guide/…
    – Smartie
    Jan 26 '16 at 10:01
  • will try this, thank you very much, I am VERY new to Mage2 - so... where you say: create etc/di.xml in your extension, does that mean in a app > design> frontend> ME> MYTHEME > etc?
    – M21
    Jan 26 '16 at 21:48
  • 1
    @CVP not at this case. You need to create a separate extension for this purpose. Here you go github.com/rogyar/FirstProduct . You need to put this extension to your Magento installation and enable it. Jan 27 '16 at 6:59
  • @YaroslavRogoza thank you very much! that looks awesome... I uploaded that and cleared the cache, but still not showing, is there some other step to 'enable' it?
    – M21
    Jan 27 '16 at 15:20
  • 1
    @CVP looks like there were extra white spaces in the code. I've just fixed this file in the repository. Please, check it out. Jan 28 '16 at 15:42

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.