8

The homepage of my magento website is showing a product list. This product list was made with the catalog product list widget configured to show the products which are in the homepage category. Products in this category have a position number. Products are displayed in what seems to be price ordered list. How do I get the widget to display position ordered list ?

2

If you want order by position in the widget product list, you must do the next.

Namespace/CustomWidgets/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogWidget\Block\Product\ProductsList">
        <plugin name="custom_widgets_product_list" type="Namespace\CustomWidgets\Plugin\Block\Product\ProductsListPlugin"/>
    </type>
</config>

Namespace/CustomWidgets/Plugin/Block/Product/ProductsListPlugin.php

<?php    
namespace Namespace\CustomWidgets\Plugin\Block\Product;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogWidget\Block\Product\ProductsList;

/**
 * Class ProductsListPlugin
 */
class ProductsListPlugin
{

    /**
     * @param ProductsList $subject
     * @param Collection $result
     * @return Collection
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterCreateCollection(ProductsList $subject, Collection $result)
    {
        $result->getSelect()->order('cat_index_position asc');

        return $result;
    }
}

This order is always by the position of the root category. If you want order by a specific category, you must create a new custom widget for that.

  • This answer helped me, thanks. I used $result->setOrder('created_at','DESC'); instead. – paul Feb 9 '19 at 20:49
1

Right, so old question but relevant for me. Sadly there wasn't an answer so I had to fix it myself and figure I'll answer the question while I'm at it.

I'll document the work here, but the entire module is available at https://github.com/rianorie/magento2-sortcatalogwidget.

First things first, I poked around at the catalogwidget module in Magento and found \Magento\CatalogWidget\Block\Product\ProductsList::createCollection. Which confirms there's no sorting functionality available. So, in comes a Plugin:

class AfterCreateCollection
{
    public function aftercreateCollection($subject, $result)
    {
        /**
         * @var \Magento\Catalog\Model\ResourceModel\Product\Collection $result
         * @var \Magento\CatalogWidget\Block\Product\ProductsList $subject
         */

        // if there's a sort_by attribute defined, add a sort to the collection
        if ($subject->hasData('sort_by')) {

            // if there's a direction given, check and use that otherwise  use the default
            $direction = strtoupper($subject->getData('sort_direction'));
            if (!in_array($direction, [Select::SQL_DESC, Select::SQL_ASC])) {
                $direction = Select::SQL_DESC;
            }

            $result->setOrder($subject->getData('sort_by'), $direction);
        }

        return $result;
    }
}

This is a good start, but the admin doesn't allow to manually add attributes to a widget definition very easily. So, we add a definition for that as well.

In etc/widget.xml we do:

<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget id="products_list">
        <parameters>
            <parameter name="sort_by" xsi:type="text" required="false" visible="true">
                <label translate="true">Attribute to sort the products by</label>
            </parameter>
            <parameter name="sort_direction" xsi:type="select" visible="true" required="false"
                       source_model="Elastomatic\SortCatalogWidget\Model\Config\Source\Direction">
                <label translate="true">Sort direction</label>
            </parameter>
        </parameters>
    </widget>
</widgets>

Aaand voila! Sorting for the catalog widget is now possible. I might add a dropdown for the product attribute field instead of the free-typing input in the module at some point, but this serves my purpose for now.

  • Since Magento catalog list products widget doesn't support Sort by position, I have installed the above-said module but still, the products are not listing by position order. Please guide me how did you make this work. – Siva Sep 7 '18 at 8:27
  • Still, the products are displaying depends on Product ID value instead of position. – Siva Sep 7 '18 at 9:11
0

Here is a solution if Rian's answer doesn't work for you:

Replace the line

$result->setOrder($subject->getData('sort_by'), $direction);

with

$result->getSelect()->reset(\Zend_Db_Select::ORDER);
$result->addAttributeToSort($subject->getData('sort_by'), $direction);

and the module should work as expected.

The updated Magento 2 module is available here: https://github.com/Bitpolar-Tech/module-sort-catalog-widget

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