1

I have added positions to the products in category in the backend.

enter image description here

This position is working in Category page in Frontend. But this position of product is not working in Product list widget inside a CMS page.

enter image description here

How can this value of position be used in Product list widget?

This is value is stored in this table in magento2 database. enter image description here

2
  • If you product list widget then from which category you will pull the positions because one product could be assigned to multiple categories? Commented Feb 12, 2020 at 13:05
  • Conditions If ALL of these conditions are TRUE : Category is 17 Commented Feb 12, 2020 at 13:35

1 Answer 1

0

When I var_dump() my grid Product object, I found some data related to "cat_index_position" in it. This is actually the piece of data I had to use to sort my product list. enter image description here

enter image description here

So I run two for each loop to sort again my product array and I get my products positioned according to the value given to on Category settings in Magento admin.

$items          = $block->getProductCollection()->getItems();
var_dump($items);exit;

$CatIndexPosition= array();
$positioned_items= array();
foreach ($items as $_item): 
    $CatIndexPosition[$_item->getId()]=$_item->getCatIndexPosition();
endforeach ;
//print_r($CatIndexPosition);echo'<br>';//exit;
$zeroadjust = max(array_keys($CatIndexPosition))+1;
//echo $zeroadjust;exit;
foreach ($CatIndexPosition as $key => $value): 
    if($value=='0'){
        $positioned_items[$zeroadjust]=$items[$key];
        $zeroadjust++;
    }
    else{
        $positioned_items[$value]=$items[$key];
    }
endforeach ;
ksort($positioned_items);

Now in our loop i replaced $items variable with $positioned_items which i have created.

<?php foreach ($positioned_items as $_item): ?>
    <div class="col-sm-6 <?php if($iterator==0){?>p-l<?php }?>">
        <div class="img-box">
2
  • nice answer but i have add code in 2.3.5 version and it is not working it any other change need? Commented Jan 1, 2021 at 6:16
  • I have no idae about it, but you can check the code inside this file in magento 2.3 and then do adjustments. Commented Jan 4, 2021 at 6:55

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.