Jump to content

PS 1.6.x.x Vistors can't find products by search


cracked

Recommended Posts

Hello guys,

 

it's me again, today with another definitive bug report for prestashop 1.6.x.x (even the most recent version 1.6.1.4 does no better). 

 

We recently hit the problem that our users / visitors couldn't find products when using prestashops standard search function. Everything in the backend was set up correctly and for logged in users everything works as expected, so I needed to debug this case.

Fortunately it wasn't hard to find. 

In Prestashops backend you have the following option:

post-1353562-0-14956400-1497963351_thumb.png

post-1353562-0-75790600-1497963351_thumb.png

 

As you can see, you are able to define which customer group is used for a customer when he is not logged in (visitor). In our case we chose the group "business customers" with id=5. You can define this for a specific shop or all shops, but this doesn't matter - because it doesn't work. 

And I can show you why:

 

In class SearchCore / Search.php you'll find the following code:

        $sql_groups = '';
        if (Group::isFeatureActive()) {
            $groups = FrontController::getCurrentCustomerGroups();
            $sql_groups = 'AND cg.`id_group` '.(count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
        }

The problem lies in FrontController::getCurrentCustomerGroups();

    /**
     * Sets and returns customer groups that the current customer(visitor) belongs to.
     *
     * @return array
     * @throws PrestaShopDatabaseException
     */
    public static function getCurrentCustomerGroups()
    {
        if (!Group::isFeatureActive()) {
            return array();
        }

        $context = Context::getContext();
        if (!isset($context->customer) || !$context->customer->id) {
            return array();
        }

        if (!is_array(self::$currentCustomerGroups)) {
            self::$currentCustomerGroups = array();
            $result = Db::getInstance()->executeS('SELECT id_group FROM '._DB_PREFIX_.'customer_group WHERE id_customer = '.(int)$context->customer->id);
            foreach ($result as $row) {
                self::$currentCustomerGroups[] = $row['id_group'];
            }
        }

        return self::$currentCustomerGroups;
    }

Here you can see on line 1203 that if "$context->customer" is not set, and neither "$context->customer->id" is, then an empty array is returned.

Back to Search.php:

 $sql_groups = 'AND cg.`id_group` '.(count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');

If $groups is empty, then 1 will be used. 

Group 1 is the standard visitors group. So this works fine, as long as you use the standard visitors group.

 

But if you define another visitors group like in our case the "business customers"-group with id 5, then this has no effect.

 

And its clearly a bug.

 

Thanks and regards,

cracked

Edited by cracked (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...