1

By default in Magento 2, the category links look like this link array

They lead to ?catId=xxx, instead of the better SEO friendly links, like the product name.

I found a solution for this issue, but it's for magento 1, and it look like this: I found this here solution link

the orignal code:

public function getUrl()
{
    $query = array(
        $this->getFilter()->getRequestVar()=>$this->getValue(),
        Mage::getBlockSingleton('page/html_pager')->getPageVarName() => null // exclude current page from urls
    );
    return Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
}

and then the custom code to solve the issue:

public function getUrl()
{
    if($this->getFilter()->getRequestVar() == "cat"){
        $category_url = Mage::getModel('catalog/category')->load($this->getValue())->getUrl();
        $return = $category_url;
        $request = Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
        if(strpos($request,'?') !== false ){
            $query_string = substr($request,strpos($request,'?'));
        }
        else{
            $query_string = '';
        }
        if(!empty($query_string)){
            $return .= $query_string;
        }
        return $return;
    }
    else{
        $query = array(
            $this->getFilter()->getRequestVar()=>$this->getValue(),
            Mage::getBlockSingleton('page/html_pager')->getPageVarName() => null // exclude current page from urls
        );

        return Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
    }
}

For magento 2, we have the same function in vendor\magento\module-catalog\Model\Layer\Filter\Item.php we have getUrl() function

Dose anyone know what it's the equivalent code for magento 2?

0

It's possible you can use this code for magento 2.

For magento 2, we have the same function in vendor\magento\module-catalog\Model\Layer\Filter\Item.php we have getUrl() function.

the orignal code:

public function getUrl()
    {
        $query = [
            $this->getFilter()->getRequestVar() => $this->getValue(),
            // exclude current page from urls
            $this->_htmlPagerBlock->getPageVarName() => null,
        ];
        return $this->_url->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, '_query' => $query]);
    }

and then the custom code to solve the issue:

public function getUrl()
    {
        if($this->getFilter()->getRequestVar() == "cat"){
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $category_url = $objectManager->create('Magento\Catalog\Model\Category')->load($this->getValue())->getUrl();
            $return = $category_url;
            $request = $this->_url->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
            if(strpos($request,'?') !== false ){
                $query_string = substr($request,strpos($request,'?'));
            }
            else{
                $query_string = '';
            }
            if(!empty($query_string)){
                $return .= $query_string;
            }
            return $return;
        }
        else{
            $query = array(
                $this->getFilter()->getRequestVar()=>$this->getValue(),
                $this->_htmlPagerBlock->getPageVarName() => null // exclude current page from urls
            );

            return $this->_url->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
        }
    }

It's working only category section. It's working fine in my case.

New contributor
Sohel Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.