Skip to main content
Tweeted twitter.com/StackMagento/status/1468279142995177476
More accurate description of the problem
Source Link
Key Shang
  • 3.4k
  • 1
  • 34
  • 59

Magento 2: OrderCustom sort order for product collection by the order string(skus, ids etc) given, not DESC or ASC

For example, I want collection list in order "sku2,sku3,sku1", but it list as "sku1,sku2", because the id of sku1 is smallernot simple DESC or ASC order. How to achieve it?

Example codes is below:

$skus = 'sku2,sku3,sku1';
$collection->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $skus)
    );

Magento 1.x has a way here How to specify custom sort order for product collection?, but it doesn't work on Magento 2:

$collection->getSelect()->order(new Zend_Db_Expr("FIELD(e.sku, $skus)"));

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

Magento 2: Order product collection by the order string(skus, ids etc) given, not DESC or ASC

For example, I want collection list in order "sku2,sku1", but it list as "sku1,sku2", because the id of sku1 is smaller. How to achieve it?

Example codes is below:

$skus = 'sku2,sku1';
$collection->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $skus)
    );

Magento 1.x has a way here How to specify custom sort order for product collection?, but it doesn't work on Magento 2:

$collection->getSelect()->order(new Zend_Db_Expr("FIELD(e.sku, $skus)"));

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

Magento 2: Custom sort order for product collection(skus, ids etc)

For example, I want collection list in order "sku2,sku3,sku1", it is not simple DESC or ASC order. How to achieve it?

Example codes is below:

$skus = 'sku2,sku3,sku1';
$collection->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $skus)
    );

Magento 1.x has a way here How to specify custom sort order for product collection?, but it doesn't work on Magento 2:

$collection->getSelect()->order(new Zend_Db_Expr("FIELD(e.sku, $skus)"));

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

remove extra question
Source Link
Key Shang
  • 3.4k
  • 1
  • 34
  • 59

For example, I want collection list in order "sku2,sku1", but it list as "sku1,sku2", because the id of sku1 is smaller. How to achieve it?

Example codes is below:

$skus = 'sku2,sku1';
$collection->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $skus)
    );

Magento 1.x has a way here How to specify custom sort order for product collection?, but it doesn't work on Magento 2:

$collection->getSelect()->order(new Zend_Db_Expr("FIELD(e.sku, $skus)"));

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

Additional problem (when sku has a hyphen(-) in it)

Thanks for the answer, the Zend_Db_Expr is working now by add "/" before "Zend_Db_Expr": $collection->getSelect()->order(new /Zend_Db_Expr("FIELD(e.sku, $skus)"));

But when sku has a hyphen(-) in it, it will result a new question Product collection sort order by custom skus string, when the sku has a hyphen in it, it will result a error , hope someone could answer it.

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

For example, I want collection list in order "sku2,sku1", but it list as "sku1,sku2", because the id of sku1 is smaller. How to achieve it?

Example codes is below:

$skus = 'sku2,sku1';
$collection->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $skus)
    );

Magento 1.x has a way here How to specify custom sort order for product collection?, but it doesn't work on Magento 2:

$collection->getSelect()->order(new Zend_Db_Expr("FIELD(e.sku, $skus)"));

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

Additional problem (when sku has a hyphen(-) in it)

Thanks for the answer, the Zend_Db_Expr is working now by add "/" before "Zend_Db_Expr": $collection->getSelect()->order(new /Zend_Db_Expr("FIELD(e.sku, $skus)"));

But when sku has a hyphen(-) in it, it will result a new question Product collection sort order by custom skus string, when the sku has a hyphen in it, it will result a error , hope someone could answer it.

For example, I want collection list in order "sku2,sku1", but it list as "sku1,sku2", because the id of sku1 is smaller. How to achieve it?

Example codes is below:

$skus = 'sku2,sku1';
$collection->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $skus)
    );

Magento 1.x has a way here How to specify custom sort order for product collection?, but it doesn't work on Magento 2:

$collection->getSelect()->order(new Zend_Db_Expr("FIELD(e.sku, $skus)"));

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

correct spelling
Source Link
Key Shang
  • 3.4k
  • 1
  • 34
  • 59

For example, I want collection list in order "sku2,sku1", but it list as "sku1,sku2", because the id of sku1 is smaller. How to achieve it?

Example codes is below:

$skus = 'sku2,sku1';
$collection->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $skus)
    );

Magento 1.x has a way here How to specify custom sort order for product collection?, but it doesn't work on Magento 2:

$collection->getSelect()->order(new Zend_Db_Expr("FIELD(e.sku, $skus)"));

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

Additional problem (when sku has a dashhyphen(-) in it)

Thanks for the answer, the Zend_Db_Expr is working now by add "/" before "Zend_Db_Expr": $collection->getSelect()->order(new /Zend_Db_Expr("FIELD(e.sku, $skus)"));

But when sku has a dashhyphen(-) in it, it will result a new question Product collection sort order by custom skus string, when the sku has a hyphen in it, it will result a error , hope someone could answer it.

For example, I want collection list in order "sku2,sku1", but it list as "sku1,sku2", because the id of sku1 is smaller. How to achieve it?

Example codes is below:

$skus = 'sku2,sku1';
$collection->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $skus)
    );

Magento 1.x has a way here How to specify custom sort order for product collection?, but it doesn't work on Magento 2:

$collection->getSelect()->order(new Zend_Db_Expr("FIELD(e.sku, $skus)"));

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

Additional problem (when sku has a dash in it)

Thanks for the answer, the Zend_Db_Expr is working now by add "/" before "Zend_Db_Expr": $collection->getSelect()->order(new /Zend_Db_Expr("FIELD(e.sku, $skus)"));

But when sku has a dash in it, it will result a new question Product collection sort order by custom skus string, when the sku has a hyphen in it, it will result a error , hope someone could answer it.

For example, I want collection list in order "sku2,sku1", but it list as "sku1,sku2", because the id of sku1 is smaller. How to achieve it?

Example codes is below:

$skus = 'sku2,sku1';
$collection->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $skus)
    );

Magento 1.x has a way here How to specify custom sort order for product collection?, but it doesn't work on Magento 2:

$collection->getSelect()->order(new Zend_Db_Expr("FIELD(e.sku, $skus)"));

Fatal error: Uncaught Error: Class '.../Zend_Db_Expr' not found in ...

Additional problem (when sku has a hyphen(-) in it)

Thanks for the answer, the Zend_Db_Expr is working now by add "/" before "Zend_Db_Expr": $collection->getSelect()->order(new /Zend_Db_Expr("FIELD(e.sku, $skus)"));

But when sku has a hyphen(-) in it, it will result a new question Product collection sort order by custom skus string, when the sku has a hyphen in it, it will result a error , hope someone could answer it.

add additional problem
Source Link
Key Shang
  • 3.4k
  • 1
  • 34
  • 59
Loading
add more details
Source Link
Key Shang
  • 3.4k
  • 1
  • 34
  • 59
Loading
Source Link
Key Shang
  • 3.4k
  • 1
  • 34
  • 59
Loading