0

I want to get category name by current url in the observer. I have tried most common code but not working, so I am trying to get category name or id by current url. For example, current url is : http://example.php/food.html. Here I want category id by this url.

I have tried this in my observer :

namespace Mymodule\Event\Observer\Logger;
use Magento\Framework\Registry;

class GlobalLogger implements ObserverInterface
{
    protected $registry;

    public function __construct(
        \Magento\Framework\Registry $registry
) {
        $this->registry = $registry;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    { 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
        echo $category->getId();
        echo $category->getName();
    }
}

Anyone have an idea for that, please?

  • Its working for me fine in category page where are you checking this code, in which page? – Rakesh Jesadiya Jan 2 '18 at 10:54
  • can u share your observer code? – Abdul Jan 2 '18 at 10:56
  • i have updated my code, kindly please check, i am using this. – Ravi Varma Jan 2 '18 at 11:08
2

You just need to check first whether current request is from category page and if from category then you need to call below code,

namespace Mymodule\Event\Observer\Logger;
use Magento\Framework\Registry;
class GlobalLogger implements ObserverInterface
{
    protected $registry;
    protected $request;

    public function __construct(
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->registry = $registry;
        $this->request = $request;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    { 
        if($this->getRequest()->getActionName() == 'category'){ 
            $category = $this->registry->registry('current_category');//get current category
            echo $category->getId();
            echo $category->getName();
        }
     }
 }

Remove var and generated folder from root and check again.

  • please let me know if you have issue – Rakesh Jesadiya Jan 2 '18 at 11:23
  • i have removed var and generated folder, and used above, but not responding. i have checked current request is catalog_category_view of action name – Ravi Varma Jan 2 '18 at 11:43
  • can we get current category in this action "catalog_category_view" – Ravi Varma Jan 2 '18 at 12:43
  • i havent try with that might be getting using those action – Rakesh Jesadiya Jan 2 '18 at 12:47
0

Try below code:

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Mymodule\Event\Observer\Logger;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;   

/**
 * Class GlobalLogger
 */
class GlobalLogger implements ObserverInterface
{

    /**
    * @param \Magento\Framework\Registry $registry
    */

    protected $registry;
    protected $request;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\Registry $registry
    ) {
        $this->request      = $request;
        $this->registry = $registry;
    }

    /**
     * Set new customer group to all his quotes
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $category = $this->registry->registry('current_category');
        if($category) {
          echo $category->getName();
          die;
        }
    }
}

After run below compiler command

php bin/magento setup:di:compile

  • Thanks for your reply, as i apply this then getting blank screen. i have updated my code, kindly please check, i am using this. – Ravi Varma Jan 2 '18 at 11:09
  • on which event your code put. can give your event? – Abdul Jan 2 '18 at 11:11
  • I am using "predispatch event". – Ravi Varma Jan 2 '18 at 11:14
  • Update ans pls check and let me know any issue – Abdul Jan 2 '18 at 11:23
  • i have used above code and run this command, i think code is not coming in this code if($category) { echo $category->getName(); die; } – Ravi Varma Jan 2 '18 at 11:36

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.