0

In Magento 2.3.0, I try to change position for category products.

In admin, each position is set but in the frontend, nothing changes. Even after clear/flush cache and reindexing.

Do you know how and in which file makes this change programmatically to get product collection sort by product position set in the category section?

In template file where I display product it's done like this :

        /**
         * Product list template
         *
         * @var $block \Magento\Catalog\Block\Product\ListProduct
         */
        $_productCollection = $block->getLoadedProductCollection();

        foreach ($_productCollection as $_product): ?>

           <!-- HTML code here -->

        <?php endforeach ?>

I try add this before foreach section but it doesn't work :

$_productCollection->addAttributeToSort('cat_index_position', 'ASC');

Also I found this code in catalog_category_view.xml file :

<block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
                    <action method="setSortBy">
                        <argument name="dir" xsi:type="string">date</argument>
                    </action>
                    <action method="setDefaultDirection">
                        <argument name="dir" xsi:type="string">asc</argument>
                    </action>

So I try to remove both <action> blocks, or change date by position but it's not better.

This is the (string)$_productCollection->getSelect() result :

    SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `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`, `stock_status_index`.`stock_status` AS `is_salable` 
FROM `catalog_product_entity` AS `e` 
INNER JOIN `catalog_category_product_index_store1` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.visibility IN(2, 4) AND cat_index.category_id='83' 
INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0 
INNER JOIN `search_tmp_5ec3b7c70e9488_09559138` AS `search_result` ON e.entity_id = search_result.entity_id 
INNER JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id 
ORDER BY `e`.`entity_id` DESC

Thanks

  • Did you also check in the admin if the category default sort is set to position? – SantiBM May 19 at 9:58
  • Yes it's set to position – Fbr May 19 at 10:04
  • Are you in the right template? Maybe another theme/module around overriding that one? You can try to debug that select: ```` $logger = new \Zend\Log\Logger(); $logger->addWriter(new \Zend\Log\Writer\Stream(BP.'/var/log/select.log')); $logger->debug((string)$_productCollection->getSelect()); ```` – SantiBM May 19 at 10:23
  • Yes I'm in the right template. Please see my edit I add more information – Fbr May 19 at 10:28
  • Can you log/print your select to see what is the sort being applied? Maybe you are overriding in at the category level or as you saw with an xml – SantiBM May 19 at 10:38

Your Answer

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

Browse other questions tagged or ask your own question.