Jump to content
coxmin

Random products on category page

Recommended Posts

Hello guys,

 

 

 

I would like that the products show random order on each category page refresh..

Can anyone help me with this ?

 

 

Thank you!

 

Share this post


Link to post
Share on other sites

You can try to change the line that get product list as following in file /controllers/front/CategpryController.php

 
 
From:
           $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay);
 
To:
            $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay, false, true, true);

Share this post


Link to post
Share on other sites

That code change should work, but please note that you need to disable caching on the Advanced Parameters > Performance tab, otherwise you'll get the same set of random products each time you refresh the page.

Share this post


Link to post
Share on other sites

Hello guys,

 

I did change the line as you told me but still not work. The cache was deactivated also.

I am using custom theme, is that the problem ?

 

Thank you,

Cosmin

Share this post


Link to post
Share on other sites

This the last part of the code

    /**
     * Assigns product list template variables
     */
    public function assignProductList()
    {
        $hook_executed = false;
        Hook::exec('actionProductListOverride', array(
            'nbProducts'   => &$this->nbProducts,
            'catProducts'  => &$this->cat_products,
            'hookExecuted' => &$hook_executed,
        ));

        // The hook was not executed, standard working
        if (!$hook_executed) {
            $this->context->smarty->assign('categoryNameComplement', '');
            $this->nbProducts = $this->category->getProducts(null, null, null, $this->orderBy, $this->orderWay, true);
            $this->pagination((int)$this->nbProducts); // Pagination must be call after "getProducts"
            $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay, false, true, true);
        }
        // Hook executed, use the override
        else {
            // Pagination must be call after "getProducts"
            $this->pagination($this->nbProducts);
        }
        
        $this->addColorsToProductList($this->cat_products);

        Hook::exec('actionProductListModifier', array(
            'nb_products'  => &$this->nbProducts,
            'cat_products' => &$this->cat_products,
        ));

        foreach ($this->cat_products as &$product) {
            if (isset($product['id_product_attribute']) && $product['id_product_attribute'] && isset($product['product_attribute_minimal_quantity'])) {
                $product['minimal_quantity'] = $product['product_attribute_minimal_quantity'];
            }
        }
        
        $this->context->smarty->assign('nb_products', $this->nbProducts);
    }

    /**
     * Returns an instance of the current category
     *
     * @return Category
     */
    public function getCategory()
    {
        return $this->category;
    }
}

Share this post


Link to post
Share on other sites

Reading the code, there must a module that is overwriting the product list on these lines:

        Hook::exec('actionProductListModifier', array(
            'nb_products'  => &$this->nbProducts,
            'cat_products' => &$this->cat_products,
        ));

You should check Modules > Positions to find out what modules are installed in the actionProductListModifier hook. One of those modules must be overwriting the product list.

Share this post


Link to post
Share on other sites

If you are using block layered navigation module, then the product list generation was hijacked by that module.

You need to modify the following file

 

/modules/blocklayered/blocklayered.php 

Share this post


Link to post
Share on other sites
	// Inform the hook was executed
		$params['hookExecuted'] = true;
		// List of product to overrride categoryController
		$params['catProducts'] = array();
		$selected_filters = $this->getSelectedFilters();
		$filter_block = $this->getFilterBlock($selected_filters);
		$title = '';

		if (is_array($filter_block['title_values']))
			foreach ($filter_block['title_values'] as $key => $val)
				$title .= ' > '.$key.' '.implode('/', $val);

		$smarty->assign('categoryNameComplement', $title);
		$this->getProducts($selected_filters, $params['catProducts'], $params['nbProducts'], $p, $n, $pages_nb, $start, $stop, $range);
		// Need a nofollow on the pagination links?
		$smarty->assign('no_follow', $filter_block['no_follow']);
	}

I opened /modules/blocklayered/blocklayered.php . Should i modify something from above ?

Share this post


Link to post
Share on other sites

You need to make change to method getProductByFilters() of this module

to make the query of product to order by a random number.

Edited by shokinro (see edit history)

Share this post


Link to post
Share on other sites

And changing with this

getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay, false, true, true);

it will work ?

Share this post


Link to post
Share on other sites

No, because it used different method, you have to modify the query itself, not the parameters of the method.

Share this post


Link to post
Share on other sites

Hello guys , the solution is that u must shuffel the products before return it at line  2092
...
 

            GROUP BY product_shop.id_product
                ORDER BY '.Tools::getProductsOrder('by', Tools::getValue('orderby'), true).' '.Tools::getProductsOrder('way', Tools::getValue('orderway')).' , cp.id_product'.
                ' LIMIT '.(((int)$this->page - 1) * $n.','.$n), true, false);
            }
        }

        if (Tools::getProductsOrder('by', Tools::getValue('orderby'), true) == 'p.price')
            Tools::orderbyPrice($this->products, Tools::getProductsOrder('way', Tools::getValue('orderway')));
            shuffle ($this->products);
        return $this->products;
    }

Share this post


Link to post
Share on other sites

In category.php is the "RAND ()" option as a filter to order the products but the pagination doesn't work.

It is possible to mix the products randomly and that the pagination also works?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More