0

I am working on Magento 2.4. I need some customisation in that I need to reset previous sorting and I need to set new 'Order By' column. I am using $collection->getSelect()->reset(Zend_Db_Select::ORDER); but it is not working and I am getting following error :

main.CRITICAL: Error: Class 'Vendor\Module\Plugin\Zend_Db_Select' not found in /app/code/Vendor/Module/Plugin/Layer.php:174

How to use reset sort column ? Any solution ?

UPDATE

I got the solution for above question and used the following

$result_main->getSelect()->reset(\Magento\Framework\DB\Select::ORDER);
$result_main->getSelect()->order(new \Zend_Db_Expr("FIELD(e.entity_id, " . implode(",", $id_order) . ")"));

But it is not sorting based on given ids.

I also print the query :

SELECT e.*, price_index.price, price_index.tax_class_id, price_index.final_price, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS minimal_price, price_index.min_price, price_index.max_price, price_index.tier_price FROM catalog_product_entity AS e INNER JOIN catalog_product_index_price AS price_index ON price_index.entity_id = e.entity_id AND price_index.customer_group_id = 0 AND price_index.website_id = '1' INNER JOIN catalog_product_website AS product_website ON product_website.product_id = e.entity_id AND product_website.website_id = 1 ORDER BY FIELD(e.entity_id, 2521, 10, 4444, 1286, 4396, 4397, 656, 4445, 4307, 3110, 1276, 9, 4446, 1280, 1611, 155, 286, 5319, 4395, 4392, 4399, 4401, 6424, 1445, 2967, 1281, 1278, 6188, 4398, 4389, 4393, 3167, 4944, 4571, 4388, 4390, 4391, 4394, 4950, 4945, 1529, 4400, 1526, 1525, 1523, 4948, 4947, 1527, 4306, 4943, 1709, 4372, 4447, 6189, 1444, 3539, 4566, 3534, 3538, 3535, 3536, 3537, 5507, 4402, 1524, 1123, 4760, 5777, 5384, 1792, 4512, 3374, 4552, 4882, 4538, 4885, 5775, 1578, 6567, 5332, 4368, 1561, 1559, 4522, 5968, 6377, 5776, 5778, 1577, 6379, 6466, 6384, 5965, 6381, 4508, 4570, 4886, 1793, 4507, 3375, 4521, 4506, 5782, 3540, 4880, 1446, 5785, 4540, 4884, 4539, 4883, 5784, 5783, 4547, 6398, 6200, 4879, 4881, 4548, 4546, 1809, 5779, 5780, 5786, 4467, 5781, 6136, 3508, 3507, 3844, 4858, 4857, 6382, 5034, 4437, 6378, 4473, 6383, 6380, 5616, 5619, 5620, 5621, 4468, 6572, 6394, 4365, 6479, 4370, 4303, 4511, 4935, 4574, 4517, 4524, 4525, 1565, 1566, 1567, 4373, 4513, 4510, 4369, 6140, 4509, 309, 3509, 4471, 4577, 4472, 3468, 3469, 4573, 4470, 4366, 1770, 3412, 4526, 4531, 6267, 6471, 4533, 4532, 4535, 4534, 6026, 4958, 4939, 4940, 4527, 4720, 4528, 4523, 5189, 6559, 1482, 1483, 6201, 6482, 6596, 4519, 1336, 6387, 6585, 4576, 4500, 1568, 4520, 4010, 4367, 4376, 4371, 1105, 4374, 4572, 3492, 1560, 1562, 6472, 6369, 5573, 4575, 198, 199, 200, 201, 1564, 4375, 5265, 6397, 1768, 6598, 4567, 6628, 1810, 1257, 1811, 4329, 4514, 4469, 4518, 1000, 3506, 4012, 1253, 1254, 1255, 1256, 1258, 1252, 1259, 1260, 1261, 1262, 1263, 1265, 1266, 1267, 1268, 1269, 1270, 1558, 6345)

1

Try using it as described below:

RESETTING THE ORDER

$collection->getSelect()->reset(\Magento\Framework\DB\Select::ORDER);

ADDING PREDEFINED ORDER TO COLLECTION

$collection->getSelect()->order(new \Zend_Db_Expr('FIELD(e.entity_id, ' . implode(',', $allIds) . ')'));

FILTER BY SPECIFIC IDS AND SORT BY IDS

if you want to limit the collection to include only products with specific IDs, then you should use

$collection->addAttributeToFilter('entity_id', ['in' => $allIds]);
$collection->setOrder('entity_id', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);

ADDING PREDEFINED ORDER TO COLLECTION AND SORT BY THE SAME COLUMN

otherwise, if you still need to use the initial approach with the ->order() method, then try appending the regular order after the above lines of code and see what happens:

$collection->getSelect()->reset(\Magento\Framework\DB\Select::ORDER);
$collection->getSelect()->order(new \Zend_Db_Expr('FIELD(e.entity_id, ' . implode(',', $allIds) . ')'));
$collection->setOrder('entity_id', \Magento\Framework\Data\Collection::SQL_ASC);
10
  • Thanks it worked, but after reset I added sorting $result_main->getSelect()->order(new \Zend_Db_Expr("FIELD(e.entity_id, all ids)")); Mar 31 at 11:45
  • and the above is not working it is not sorting based on Id which I passed Mar 31 at 11:53
  • is 'all ids' meant to be an array of IDs? if so, please my updated answer on how to add the order to the collection based on your values. Mar 31 at 11:58
  • Yes I did the same but still not working. $result_main->getSelect()->reset(\Magento\Framework\DB\Select::ORDER); $result_main->getSelect()->order(new \Zend_Db_Expr("FIELD(e.entity_id, " . implode(",", $id_order) . ")")); Mar 31 at 12:11
  • on a second thought, i guess you are actually trying to do something else. when you use ->order() with a Zend expression like this, you are telling the collection that you want the results ordered in the order provided by the array in the expression. what i think you initially wanted was to filter the collection to include only certain IDs and then order it by entity ID. can you confirm? i also updated my answer, maybe it will help you. Mar 31 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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