Using foreach change the position of my product collection
-
14-04-2021 - |
Question
The best way to explain this issue is showing an example:
In the product list.phtml I had the following
<?php
$logger->info($_productCollection->getData());
foreach ($_productCollection as $_product){
$logger->info('id product ' . $_product->getEntityId());
}
foreach ($_productCollection->getData() as $_product){
$logger->info('id product with array ' . $_product['entity_id']);
}
?>
My collection has the next data
array (
0 =>
array (
'entity_id' => '172',
'sku' => '139373',
'cat_index_position' => '0'
),
1 =>
array (
'entity_id' => '147',
'sku' => '144486',
'cat_index_position' => '5'
),
2 =>
array (
'entity_id' => '148',
'sku' => '144485',
'cat_index_position' => '6',
)
)
The first foreach I get the next log
id product 147
id product 148
id product 172
In the second foreach:
id product with array 172
id product with array 147
id product with array 148
The first foreach is changing the position of my collection regarding the 'entity_id'. The second foreach is the order that I want but I need the $_product object and not the array.
Why my collection is changing the order of my products?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange