0

I want to sort products with custom value (1) numeric attribute. In toolbar.php:

if ($this->getCurrentOrder()) {
    $this->_collection->addAttributeToSort(
        $this->getCurrentOrder(),
        $this->getCurrentDirection()
    );
    $this->_collection->getSelect()->order('recomendado_vistas', 1);
}

but this not work, it is possible to sort product with custom numeric value (1) attribute first?

2 Answers 2

0

If Have you tried using

$this->_collection->setOrder('recomendado_vistas', 'ASC');

or if recomendado_vistas values are mix(number, char etc) you can set temporary that not 1 to other value and sort it

$this->_collection->addAttributeToSelect('recomendado_vistas', 'left');
$this->_collection->getSelect()->order(new \Zend_Db_Expr("CASE WHEN recomendado_vistas != 1 THEN 9999 ELSE recomendado_vistas END ASC"));
0

You can use ORDER BY FIELD in your collection

Sample code:

if($this->getCurrentOrder()) {
    $this->_collection->addAttributeToSort(
        $this->getCurrentOrder(),
        $this->getCurrentDirection()
    );
    $this->_collection->getSelect()->order(new \Zend_Db_Expr("FIELD(recomendado_vistas, 1)"));
}

You can pass multiple values in it if you want, like this:

$this->_collection->getSelect()->order(new \Zend_Db_Expr("FIELD(recomendado_vistas, 1, 3, 6)"));

Make sure your collection is getting your columns recomendado_vistas. You can debug your query using this code:

echo $this->_collection->getSelect();
1
  • i tried but it does not work, and logs do not give error, the page display normal order in all categories and attribute "recomendado_vistas" is set because i tried "$this->_collection->addAttributeToSort('recomendado_vistas','asc');" and it works but i need only this attribute with value (1), any ideas? Commented Feb 10, 2022 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.