Magento 2 - catalog product list widget product order












6















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 ?










share|improve this question





























    6















    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 ?










    share|improve this question



























      6












      6








      6








      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 ?










      share|improve this question
















      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 ?







      magento2 product-list widget






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 16 '17 at 19:55









      Abhishek Panchal

      3,4233929




      3,4233929










      asked Jul 25 '16 at 8:05









      PierreGriffonPierreGriffon

      516




      516






















          2 Answers
          2






          active

          oldest

          votes


















          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 MagentoCatalogWidgetBlockProductProductsList::createCollection. Which confirms there's no sorting functionality available. So, in comes a Plugin:



          class AfterCreateCollection
          {
          public function aftercreateCollection($subject, $result)
          {
          /**
          * @var MagentoCatalogModelResourceModelProductCollection $result
          * @var MagentoCatalogWidgetBlockProductProductsList $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="ElastomaticSortCatalogWidgetModelConfigSourceDirection">
          <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.






          share|improve this answer
























          • 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.

            – Magento vsmarttec
            Sep 7 '18 at 8:27













          • Still, the products are displaying depends on Product ID value instead of position.

            – Magento vsmarttec
            Sep 7 '18 at 9:11



















          0














          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="MagentoCatalogWidgetBlockProductProductsList">
          <plugin name="custom_widgets_product_list" type="NamespaceCustomWidgetsPluginBlockProductProductsListPlugin"/>
          </type>
          </config>



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




          <?php    
          namespace NamespaceCustomWidgetsPluginBlockProduct;

          use MagentoCatalogModelResourceModelProductCollection;
          use MagentoCatalogWidgetBlockProductProductsList;

          /**
          * 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 always is by the position of the root category. If you want order by a specific category, you must create a new custom widget for that.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f127221%2fmagento-2-catalog-product-list-widget-product-order%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            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 MagentoCatalogWidgetBlockProductProductsList::createCollection. Which confirms there's no sorting functionality available. So, in comes a Plugin:



            class AfterCreateCollection
            {
            public function aftercreateCollection($subject, $result)
            {
            /**
            * @var MagentoCatalogModelResourceModelProductCollection $result
            * @var MagentoCatalogWidgetBlockProductProductsList $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="ElastomaticSortCatalogWidgetModelConfigSourceDirection">
            <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.






            share|improve this answer
























            • 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.

              – Magento vsmarttec
              Sep 7 '18 at 8:27













            • Still, the products are displaying depends on Product ID value instead of position.

              – Magento vsmarttec
              Sep 7 '18 at 9:11
















            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 MagentoCatalogWidgetBlockProductProductsList::createCollection. Which confirms there's no sorting functionality available. So, in comes a Plugin:



            class AfterCreateCollection
            {
            public function aftercreateCollection($subject, $result)
            {
            /**
            * @var MagentoCatalogModelResourceModelProductCollection $result
            * @var MagentoCatalogWidgetBlockProductProductsList $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="ElastomaticSortCatalogWidgetModelConfigSourceDirection">
            <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.






            share|improve this answer
























            • 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.

              – Magento vsmarttec
              Sep 7 '18 at 8:27













            • Still, the products are displaying depends on Product ID value instead of position.

              – Magento vsmarttec
              Sep 7 '18 at 9:11














            1












            1








            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 MagentoCatalogWidgetBlockProductProductsList::createCollection. Which confirms there's no sorting functionality available. So, in comes a Plugin:



            class AfterCreateCollection
            {
            public function aftercreateCollection($subject, $result)
            {
            /**
            * @var MagentoCatalogModelResourceModelProductCollection $result
            * @var MagentoCatalogWidgetBlockProductProductsList $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="ElastomaticSortCatalogWidgetModelConfigSourceDirection">
            <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.






            share|improve this answer













            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 MagentoCatalogWidgetBlockProductProductsList::createCollection. Which confirms there's no sorting functionality available. So, in comes a Plugin:



            class AfterCreateCollection
            {
            public function aftercreateCollection($subject, $result)
            {
            /**
            * @var MagentoCatalogModelResourceModelProductCollection $result
            * @var MagentoCatalogWidgetBlockProductProductsList $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="ElastomaticSortCatalogWidgetModelConfigSourceDirection">
            <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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 29 '18 at 13:50









            RianRian

            7951617




            7951617













            • 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.

              – Magento vsmarttec
              Sep 7 '18 at 8:27













            • Still, the products are displaying depends on Product ID value instead of position.

              – Magento vsmarttec
              Sep 7 '18 at 9:11



















            • 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.

              – Magento vsmarttec
              Sep 7 '18 at 8:27













            • Still, the products are displaying depends on Product ID value instead of position.

              – Magento vsmarttec
              Sep 7 '18 at 9:11

















            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.

            – Magento vsmarttec
            Sep 7 '18 at 8:27







            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.

            – Magento vsmarttec
            Sep 7 '18 at 8:27















            Still, the products are displaying depends on Product ID value instead of position.

            – Magento vsmarttec
            Sep 7 '18 at 9:11





            Still, the products are displaying depends on Product ID value instead of position.

            – Magento vsmarttec
            Sep 7 '18 at 9:11













            0














            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="MagentoCatalogWidgetBlockProductProductsList">
            <plugin name="custom_widgets_product_list" type="NamespaceCustomWidgetsPluginBlockProductProductsListPlugin"/>
            </type>
            </config>



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




            <?php    
            namespace NamespaceCustomWidgetsPluginBlockProduct;

            use MagentoCatalogModelResourceModelProductCollection;
            use MagentoCatalogWidgetBlockProductProductsList;

            /**
            * 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 always is by the position of the root category. If you want order by a specific category, you must create a new custom widget for that.






            share|improve this answer




























              0














              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="MagentoCatalogWidgetBlockProductProductsList">
              <plugin name="custom_widgets_product_list" type="NamespaceCustomWidgetsPluginBlockProductProductsListPlugin"/>
              </type>
              </config>



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




              <?php    
              namespace NamespaceCustomWidgetsPluginBlockProduct;

              use MagentoCatalogModelResourceModelProductCollection;
              use MagentoCatalogWidgetBlockProductProductsList;

              /**
              * 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 always is by the position of the root category. If you want order by a specific category, you must create a new custom widget for that.






              share|improve this answer


























                0












                0








                0







                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="MagentoCatalogWidgetBlockProductProductsList">
                <plugin name="custom_widgets_product_list" type="NamespaceCustomWidgetsPluginBlockProductProductsListPlugin"/>
                </type>
                </config>



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




                <?php    
                namespace NamespaceCustomWidgetsPluginBlockProduct;

                use MagentoCatalogModelResourceModelProductCollection;
                use MagentoCatalogWidgetBlockProductProductsList;

                /**
                * 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 always is by the position of the root category. If you want order by a specific category, you must create a new custom widget for that.






                share|improve this answer













                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="MagentoCatalogWidgetBlockProductProductsList">
                <plugin name="custom_widgets_product_list" type="NamespaceCustomWidgetsPluginBlockProductProductsListPlugin"/>
                </type>
                </config>



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




                <?php    
                namespace NamespaceCustomWidgetsPluginBlockProduct;

                use MagentoCatalogModelResourceModelProductCollection;
                use MagentoCatalogWidgetBlockProductProductsList;

                /**
                * 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 always is by the position of the root category. If you want order by a specific category, you must create a new custom widget for that.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 23 mins ago









                raumatbelraumatbel

                669212




                669212






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f127221%2fmagento-2-catalog-product-list-widget-product-order%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What other Star Trek series did the main TNG cast show up in?

                    Berlina muro

                    Berlina aerponto