0

on this site I use magento and theme : http://www.herbalheaven.co.uk

Everything works good but when you go on categories I got the same error for all of them : http://www.herbalheaven.co.uk/herbal-ayurveda.html

Parse error: syntax error, unexpected '' (T_NS_SEPARATOR), expecting function (T_FUNCTION) or const (T_CONST) in /home/herbalheaven/public_html/app/code/MageBig/AjaxFilter/Plugin/Catalog/Product/ProductList/Toolbar.php on line 16

And here is the code :

<?php
/**
 * Copyright © www.magebig.com - All rights reserved.
 * See LICENSE.txt for license details.
 */
namespace MageBig\AjaxFilter\Plugin\Catalog\Product\ProductList;

use MageBig\AjaxFilter\Model\Layer\Filter\Rating;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Registry;
use Magento\Store\Model\ScopeInterface;

class Toolbar
{
    private \Magento\Catalog\Model\Product\ProductList\Toolbar $toolbarModel;
    private RequestInterface $request;
    private Registry $coreRegistry;
    private ScopeConfigInterface $scopeConfig;

    public function __construct(
        \Magento\Catalog\Model\Product\ProductList\Toolbar $toolbarModel,
        ScopeConfigInterface $scopeConfig,
        Registry $coreRegistry
    ) 
    {
        $this->toolbarModel = $toolbarModel;
        $this->scopeConfig = $scopeConfig;
        $this->coreRegistry = $coreRegistry;
    }

    public function aroundSetCollection(
        \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
        \Closure $proceed,
        $collection
    ) {
        if (!$this->coreRegistry->registry('product_filter_collection')) {
            $c1 = clone $collection;
            $c1->setOrder('price', 'desc')->getFirstItem();
            $this->coreRegistry->register('product_filter_collection', $c1);
        }

        $order = $subject->getCurrentOrder();
        $result = $proceed($collection);
        $ratingCode = Rating::RATING_CODE;

        if ($ratingCode && ($order == $ratingCode)) {
            $direction = $subject->getCurrentDirection();

            $searchEngine = $this->scopeConfig->getValue('catalog/search/engine', ScopeInterface::SCOPE_STORE);
            if ($searchEngine == 'mysql') {
                $collection->setOrder('rating_summary', $direction);
            } else {
                $collection->setOrder('rating', $direction);
            }
        }

        return $result;
    }

    /**
     * @param $subject
     * @param $dir
     * @return string
     */
    public function afterGetCurrentDirection($subject, $dir)
    {
        $defaultDir = $subject->getCurrentOrder() == 'rating' ? 'desc' : $dir;
        $subject->setDefaultDirection($defaultDir);

        if (!$this->toolbarModel->getDirection()) {
            $dir = $defaultDir;
        }

        return $dir;
    }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103

1 Answers1

0

Please use below code

<?php
/**
 * Copyright © www.magebig.com - All rights reserved.
 * See LICENSE.txt for license details.
 */
namespace MageBig\AjaxFilter\Plugin\Catalog\Product\ProductList;

use MageBig\AjaxFilter\Model\Layer\Filter\Rating;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Registry;
use Magento\Store\Model\ScopeInterface;
use Magento\Catalog\Model\Product\ProductList\Toolbar as defaultToolbar;

class Toolbar
{
    private defaultToolbar $toolbarModel;
    private RequestInterface $request;
    private Registry $coreRegistry;
    private ScopeConfigInterface $scopeConfig;

    public function __construct(
        defaultToolbar $toolbarModel,
        ScopeConfigInterface $scopeConfig,
        Registry $coreRegistry
    ) 
    {
        $this->toolbarModel = $toolbarModel;
        $this->scopeConfig = $scopeConfig;
        $this->coreRegistry = $coreRegistry;
    }

    public function aroundSetCollection(
        defaultToolbar $subject,
        \Closure $proceed,
        $collection
    ) {
        if (!$this->coreRegistry->registry('product_filter_collection')) {
            $c1 = clone $collection;
            $c1->setOrder('price', 'desc')->getFirstItem();
            $this->coreRegistry->register('product_filter_collection', $c1);
        }

        $order = $subject->getCurrentOrder();
        $result = $proceed($collection);
        $ratingCode = Rating::RATING_CODE;

        if ($ratingCode && ($order == $ratingCode)) {
            $direction = $subject->getCurrentDirection();

            $searchEngine = $this->scopeConfig->getValue('catalog/search/engine', ScopeInterface::SCOPE_STORE);
            if ($searchEngine == 'mysql') {
                $collection->setOrder('rating_summary', $direction);
            } else {
                $collection->setOrder('rating', $direction);
            }
        }

        return $result;
    }

    /**
     * @param $subject
     * @param $dir
     * @return string
     */
    public function afterGetCurrentDirection($subject, $dir)
    {
        $defaultDir = $subject->getCurrentOrder() == 'rating' ? 'desc' : $dir;
        $subject->setDefaultDirection($defaultDir);

        if (!$this->toolbarModel->getDirection()) {
            $dir = $defaultDir;
        }

        return $dir;
    }
}

If this code work, I have explain how its work

Error: In your code you have added extra \ slashed in line on 16.

Step 1: First I have include default Toolbar Class with the alias of defaultToolbar.

use Magento\Catalog\Model\Product\ProductList\Toolbar as defaultToolbar;

Step 2: I have replace all manual path with the alias name.

Thanks

Waseem Ahmad
  • 303
  • 3
  • 10