0

I try to overwrite get_url() function located in item.php from this path \vendor\magento\module-catalog\Model\Layer\Filter\Item.php

For this, i created a PLUGIN, and my code it's this one:

<?php

namespace mysite\CartNotification\Plugin;

class CategoryPlugin {


    public function aftergetUrl(\Magento\Catalog\Model\Layer\Filter\Item $item)
        {
            //this code change the category links name to a freandly SEO Llinks
            if($this->getFilter()->getRequestVar() =="cat"){

                $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $object_manager = $_objectManager->create('Magento\Catalog\Model\Category')->load($this->getValue())->getUrl();
                $return = $object_manager;

                $request = $this->_url->getUrl('*/*/*', ['_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 = [
                $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]);

                }

        }
}

But i have an issue with the methods that are used in this file. When i run this code, i get err for each methods used here with this message: Fatal error: Uncaught Error: Call to undefined method Sitename\CartNotification\Plugin\CategoryPlugin::getFilter() in...

So my funciton are not define. Does anyone know how to solve this problem. and define my methods?

1

Just use :

$item->getFilter()

instead of :

$this->getFilter()
0

Try following way:

class CategoryPlugin
{
    /**
     * Url
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $url;

    /**
     *
     * @var \Magento\Catalog\Model\Category
     */
    protected $category;

    /**
     * Construct
     *
     * @param \Magento\Framework\UrlInterface $url
     * @param \Magento\Catalog\Model\Category $category
     */
    public function __construct(
        \Magento\Framework\UrlInterface $url,
        \Magento\Catalog\Model\Category $category
    ) {
        $this->url = $url;
        $this->category = $category;
    }

    public function afterGetUrl(
        \Magento\Catalog\Model\Layer\Filter\Item $item,
        $resultUrl
    ) {
        //this code change the category links name to a freandly SEO Llinks
        if($item->getFilter()->getRequestVar() == "cat") {

            $return = $this->category->load($item->getValue())->getUrl();
            $request = $this->url->getUrl('*/*/*', ['_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;
        }

        return $resultUrl;
    }
}
  • do you know why this it's working just for the first category? i mean, now all my category have the same name like the first one – Alin Lupoiu Apr 3 '17 at 11:29
  • but if a insert only the function in vendor location, this works just fine – Alin Lupoiu Apr 3 '17 at 11:30

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.