Jump to content

[Solved]Sort by(order by) product reference


presta-rocks

Recommended Posts

How to sort product by product reference?

 

I'm type in my product-sort.tpl this code

<option value="{$link->addSortDetails($request, 'reference', 'asc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'reference' AND $orderway eq 'ASC'}selected="selected"{/if}>{l s='ref: A to Z'}</option>

but it doesn't work(when i select ref it's sort by defult order)

 

Presta ver. 1.4.6.2

Link to comment
Share on other sites

Hello,

 

The value of the option should not be a link. It should be

reference:asc

 

Do you mean this:

<option value="reference:asc" {if $orderby eq 'reference' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='reference: A to Z'}</option>
  <option value="reference:desc" {if $orderby eq 'reference' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='reference: z to a'}</option>

 

It's not working :(

Link to comment
Share on other sites

You will need to edit the classes that display products, handle the situation when orderBy is reference and set the $orderByPrefix.

 

I wast trying this before i post this topic and it's was not working even if i type order by p.reference i mysql query.

But i;m using layered navigation and i finally find a solution.

 

This is what i made.

 

1. In product-sort.tpl of template im type this

  <option value="{$link->addSortDetails($request, 'reference', 'asc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'reference' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='Reference: A to Z'}</option>
  <option value="{$link->addSortDetails($request, 'reference', 'desc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'reference' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='Reference: Z to A'}</option>

 

2.In classes/Tools.php i method getProductsOrder i made this changes

 

first change

$list = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity'');

 

to

 

$list = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7 => 'reference');

 

second change

if ($value == 'id_product' || $value == 'date_add' || $value == 'date_upd' || $value == 'price')

 

to

 

if ($value == 'id_product' || $value == 'date_add' || $value == 'date_upd' || $value == 'price' || $value == 'reference')

  • Like 1
Link to comment
Share on other sites

  • 5 months later...

Thank you for sharing, marcinsz101, that works very well! Do you know how can I set a default sort type when I open the product list? That is, I don't want to display the sort bar but I want the products to be ordered by reference by default. Thanks to anyone who could help me.

Link to comment
Share on other sites

  • 2 months later...
  • 2 months later...
  • 1 month later...
  • 1 month later...

Can somebody explain to me, where to insert that 2 rows into file : "themes/default/product-sort.tpl"

 

I am using prestashop 1.5.3.1, and i insert and replace like marcinsz101 posted, but maybe somewhere i made mistake ?

 

I insert that 2 rows into product-sort-tpl and i can see in front office, but when is sort it, it automatically turn to sort by name.

 

thanks for reply.

Link to comment
Share on other sites

  • 1 month later...

Thank you for sharing this marcinsz101

 

Just for the record, It works in 1.5.3.1 as well.

 

Hi Kashibabu!

could you tell us how did you makes it works in PS 1.5.3.1? I tried but i run into the same oorbx's troubles: when i choose the "reference" option automatically the sort list turn to sort by name: A to Z

 

In product-sort.tpl I put options in this way:

 

<option value="reference:asc" {if $orderby eq 'reference' AND $orderway eq 'asc'}selected="selected"{/if}>Reference: A to Z</option>
<option value="reference:desc" {if $orderby eq 'reference' AND $orderway eq 'desc'}selected="selected"{/if}>Reference: Z to A</option>

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

Can somebody explain to me, where to insert that 2 rows into file : "themes/default/product-sort.tpl"

 

I am using prestashop 1.5.3.1, and i insert and replace like marcinsz101 posted, but maybe somewhere i made mistake ?

 

I insert that 2 rows into product-sort-tpl and i can see in front office, but when is sort it, it automatically turn to sort by name.

 

thanks for reply.

 

Hi!

try to add the new array value in function productSort() you find it in ps_root/classes/controller/FrontController.php

 

 $order_by_values = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7 => 'reference');

 

For PS's best practice you should copy and paste the whole function in "FrontController.php" file (override folder): ps_root/override/classes/controller/FrontController.php file

 

<?php
class FrontController extends FrontControllerCore
{
public function productSort()
{
 // $this->orderBy = Tools::getProductsOrder('by', Tools::getValue('orderby'));
 // $this->orderWay = Tools::getProductsOrder('way', Tools::getValue('orderway'));
 // 'orderbydefault' => Tools::getProductsOrder('by'),
 // 'orderwayposition' => Tools::getProductsOrder('way'), // Deprecated: orderwayposition
 // 'orderwaydefault' => Tools::getProductsOrder('way'),
 $stock_management = Configuration::get('PS_STOCK_MANAGEMENT') ? true : false; // no display quantity order if stock management disabled
 $order_by_values = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7 => 'reference');
 $order_way_values = array(0 => 'asc', 1 => 'desc');
 $this->orderBy = Tools::strtolower(Tools::getValue('orderby', $order_by_values[(int)Configuration::get('PS_PRODUCTS_ORDER_BY')]));
 $this->orderWay = Tools::strtolower(Tools::getValue('orderway', $order_way_values[(int)Configuration::get('PS_PRODUCTS_ORDER_WAY')]));
 if (!in_array($this->orderBy, $order_by_values))
  $this->orderBy = $order_by_values[0];
 if (!in_array($this->orderWay, $order_way_values))
  $this->orderWay = $order_way_values[0];
 $this->context->smarty->assign(array(
  'orderby' => $this->orderBy,
  'orderway' => $this->orderWay,
  'orderbydefault' => $order_by_values[(int)Configuration::get('PS_PRODUCTS_ORDER_BY')],
  'orderwayposition' => $order_way_values[(int)Configuration::get('PS_PRODUCTS_ORDER_WAY')], // Deprecated: orderwayposition
  'orderwaydefault' => $order_way_values[(int)Configuration::get('PS_PRODUCTS_ORDER_WAY')],
  'stock_management' => (int)$stock_management));
}

} // *** END Class override

 

 

If you have to change query because you added customized additional fields (for me are "author" and "year" for example) you should change query you find in ps_root/classes/Category.php and ps_root/classes/Search.php files: paste function getProducts() in ps_root/override/classes/Category.php a and function find() in ps_root/override/classes/Search.php and customize it

 

Hope this help

bye!

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

Does anyone know if is there any way to order products by their position in THE DEFAULT category.

 

I am asking because I have child categories (subcategories), and the products don't get ordered in the front as in theback.

Link to comment
Share on other sites

  • 4 weeks later...

<p>

I wast trying this before i post this topic and it&#39;s was not working even if i type order by p.reference i mysql query. But i;m using layered navigation and i finally find a solution. This is what i made. 1. In product-sort.tpl of template im type this
</p>
<option and="" eq="" if="" orderby="" orderway="" selected="selected" value="{$link->addSortDetails($request, 'reference', 'asc')|escape:'htmlall':'UTF-8'}">{l s=&#39;Reference: A to Z&#39;}</option><option and="" eq="" if="" orderby="" orderway="" selected="selected" value="{$link->addSortDetails($request, 'reference', 'desc')|escape:'htmlall':'UTF-8'}">{l s=&#39;Reference: Z to A&#39;}</option>
<p>

2.In classes/Tools.php i method getProductsOrder i made this changes first change

 $list = array(0 => &#39;name&#39;, 1 => &#39;price&#39;, 2 => &#39;date_add&#39;, 3 => &#39;date_upd&#39;, 4 => &#39;position&#39;, 5 => &#39;manufacturer_name&#39;, 6 => &#39;quantity&#39;&#39;); 

to

 $list = array(0 => &#39;name&#39;, 1 => &#39;price&#39;, 2 => &#39;date_add&#39;, 3 => &#39;date_upd&#39;, 4 => &#39;position&#39;, 5 => &#39;manufacturer_name&#39;, 6 => &#39;quantity&#39;, 7 => &#39;reference&#39;); 

second change

 if ($value == &#39;id_product&#39; || $value == &#39;date_add&#39; || $value == &#39;date_upd&#39; || $value == &#39;price&#39;) 

to

 if ($value == &#39;id_product&#39; || $value == &#39;date_add&#39; || $value == &#39;date_upd&#39; || $value == &#39;price&#39; || $value == &#39;reference&#39;) 

</p>

<p> </p>

<p> </p>

<p>it work for me using the above solution but using this</p>

 

 

<option value="reference:asc" {if $orderby eq 'reference' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='reference: A to Z'}</option>

<option value="reference:desc" {if $orderby eq 'reference' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='reference: z to a'}</option>

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

  • 5 months later...
  • 9 months later...

Do you mean this:

<option value="reference:asc" {if $orderby eq 'reference' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='reference: A to Z'}</option>
   <option value="reference:desc" {if $orderby eq 'reference' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='reference: z to a'}</option>
  
It's not working :(

 

The same like that post:

http://www.prestashop.com/forums/topic/190057-sort-products-by-id-product/

Link to comment
Share on other sites

  • 4 months later...
  • 1 year later...

Hello everyone. It is possible sort the products order by EAN-13 or UPC field??


 


I dont use this fields and I would like sort the orders invoice because my store and stock is too big. 


 


Reference numbers and another fields are disordered. I need a new field to sort.


 


Thanks and regards


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...