How set order with custom field column magento 2












3















I want to setOrder with custom field column in get list product. I override toolbar.php but it's not working



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="MagentoCatalogBlockProductProductListToolbar">
<plugin name="Namespace_Company::addPriceDecendingFilterInToolbar" type="NamespaceProductPluginProductProductListToolbar" />
</type>

</config>


NamespaceProductPluginProductProductListToolbar
in Toolbar.php



namespace NamespaceProductPluginProductProductList;
class Toolbar
{
/**
* Plugin
*
* @param MagentoCatalogBlockProductProductListToolbar $subject
* @param Closure $proceed
* @param MagentoFrameworkDataCollection $collection
* @return MagentoCatalogBlockProductProductListToolbar
*/
public function aroundSetCollection(
MagentoCatalogBlockProductProductListToolbar $subject,
Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();


$subquery = new Zend_Db_Expr("<query get min price>");



$collection->getSelect()->columns(array('price_sort' => $subquery));
//to debug



$result = $proceed($collection);

if ($currentOrder) {
if ($currentOrder == 'price_high') {
$subject->getCollection()->setOrder('price_sort', ' DESC');
# $subject->getSelect()->order('price_sort DESC');
} elseif ($currentOrder == 'price_low') {
$subject->getCollection()->setOrder('price_sort', 'asc');
}elseif ($currentOrder == 'price_low') {
$subject->getCollection()->setOrder('position', 'desc');
}elseif ($currentOrder == 'bestseller') {
$subject->getCollection()->setOrder('price', 'asc');
}
}

return $result;
}

public function aftergetAvailableOrders()
{
$sortFilter=array(
'price_high'=>__('Price High To Low'),
'price_low'=>__('Price Low To High'),
'position'=>__('Newlastest'),
'bestseller'=>__('Bestseller'),

);
return $sortFilter;
}
}


In product/list.phtml, I debug query but in order by it's not showing.



I want to show my query <query sql> order by price_sort DESC



But if in toolbar.php, I replace



$subject->getCollection()->setOrder('price_sort', ' DESC');


to



$subject->getCollection()->setOrder('price', ' DESC');


and then I debug the query, then in query it shows



order by price DESC



I want use column price_sort in setOrder.










share|improve this question
















bumped to the homepage by Community 1 min ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    3















    I want to setOrder with custom field column in get list product. I override toolbar.php but it's not working



    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="MagentoCatalogBlockProductProductListToolbar">
    <plugin name="Namespace_Company::addPriceDecendingFilterInToolbar" type="NamespaceProductPluginProductProductListToolbar" />
    </type>

    </config>


    NamespaceProductPluginProductProductListToolbar
    in Toolbar.php



    namespace NamespaceProductPluginProductProductList;
    class Toolbar
    {
    /**
    * Plugin
    *
    * @param MagentoCatalogBlockProductProductListToolbar $subject
    * @param Closure $proceed
    * @param MagentoFrameworkDataCollection $collection
    * @return MagentoCatalogBlockProductProductListToolbar
    */
    public function aroundSetCollection(
    MagentoCatalogBlockProductProductListToolbar $subject,
    Closure $proceed,
    $collection
    ) {
    $currentOrder = $subject->getCurrentOrder();


    $subquery = new Zend_Db_Expr("<query get min price>");



    $collection->getSelect()->columns(array('price_sort' => $subquery));
    //to debug



    $result = $proceed($collection);

    if ($currentOrder) {
    if ($currentOrder == 'price_high') {
    $subject->getCollection()->setOrder('price_sort', ' DESC');
    # $subject->getSelect()->order('price_sort DESC');
    } elseif ($currentOrder == 'price_low') {
    $subject->getCollection()->setOrder('price_sort', 'asc');
    }elseif ($currentOrder == 'price_low') {
    $subject->getCollection()->setOrder('position', 'desc');
    }elseif ($currentOrder == 'bestseller') {
    $subject->getCollection()->setOrder('price', 'asc');
    }
    }

    return $result;
    }

    public function aftergetAvailableOrders()
    {
    $sortFilter=array(
    'price_high'=>__('Price High To Low'),
    'price_low'=>__('Price Low To High'),
    'position'=>__('Newlastest'),
    'bestseller'=>__('Bestseller'),

    );
    return $sortFilter;
    }
    }


    In product/list.phtml, I debug query but in order by it's not showing.



    I want to show my query <query sql> order by price_sort DESC



    But if in toolbar.php, I replace



    $subject->getCollection()->setOrder('price_sort', ' DESC');


    to



    $subject->getCollection()->setOrder('price', ' DESC');


    and then I debug the query, then in query it shows



    order by price DESC



    I want use column price_sort in setOrder.










    share|improve this question
















    bumped to the homepage by Community 1 min ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      3












      3








      3








      I want to setOrder with custom field column in get list product. I override toolbar.php but it's not working



      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="MagentoCatalogBlockProductProductListToolbar">
      <plugin name="Namespace_Company::addPriceDecendingFilterInToolbar" type="NamespaceProductPluginProductProductListToolbar" />
      </type>

      </config>


      NamespaceProductPluginProductProductListToolbar
      in Toolbar.php



      namespace NamespaceProductPluginProductProductList;
      class Toolbar
      {
      /**
      * Plugin
      *
      * @param MagentoCatalogBlockProductProductListToolbar $subject
      * @param Closure $proceed
      * @param MagentoFrameworkDataCollection $collection
      * @return MagentoCatalogBlockProductProductListToolbar
      */
      public function aroundSetCollection(
      MagentoCatalogBlockProductProductListToolbar $subject,
      Closure $proceed,
      $collection
      ) {
      $currentOrder = $subject->getCurrentOrder();


      $subquery = new Zend_Db_Expr("<query get min price>");



      $collection->getSelect()->columns(array('price_sort' => $subquery));
      //to debug



      $result = $proceed($collection);

      if ($currentOrder) {
      if ($currentOrder == 'price_high') {
      $subject->getCollection()->setOrder('price_sort', ' DESC');
      # $subject->getSelect()->order('price_sort DESC');
      } elseif ($currentOrder == 'price_low') {
      $subject->getCollection()->setOrder('price_sort', 'asc');
      }elseif ($currentOrder == 'price_low') {
      $subject->getCollection()->setOrder('position', 'desc');
      }elseif ($currentOrder == 'bestseller') {
      $subject->getCollection()->setOrder('price', 'asc');
      }
      }

      return $result;
      }

      public function aftergetAvailableOrders()
      {
      $sortFilter=array(
      'price_high'=>__('Price High To Low'),
      'price_low'=>__('Price Low To High'),
      'position'=>__('Newlastest'),
      'bestseller'=>__('Bestseller'),

      );
      return $sortFilter;
      }
      }


      In product/list.phtml, I debug query but in order by it's not showing.



      I want to show my query <query sql> order by price_sort DESC



      But if in toolbar.php, I replace



      $subject->getCollection()->setOrder('price_sort', ' DESC');


      to



      $subject->getCollection()->setOrder('price', ' DESC');


      and then I debug the query, then in query it shows



      order by price DESC



      I want use column price_sort in setOrder.










      share|improve this question
















      I want to setOrder with custom field column in get list product. I override toolbar.php but it's not working



      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="MagentoCatalogBlockProductProductListToolbar">
      <plugin name="Namespace_Company::addPriceDecendingFilterInToolbar" type="NamespaceProductPluginProductProductListToolbar" />
      </type>

      </config>


      NamespaceProductPluginProductProductListToolbar
      in Toolbar.php



      namespace NamespaceProductPluginProductProductList;
      class Toolbar
      {
      /**
      * Plugin
      *
      * @param MagentoCatalogBlockProductProductListToolbar $subject
      * @param Closure $proceed
      * @param MagentoFrameworkDataCollection $collection
      * @return MagentoCatalogBlockProductProductListToolbar
      */
      public function aroundSetCollection(
      MagentoCatalogBlockProductProductListToolbar $subject,
      Closure $proceed,
      $collection
      ) {
      $currentOrder = $subject->getCurrentOrder();


      $subquery = new Zend_Db_Expr("<query get min price>");



      $collection->getSelect()->columns(array('price_sort' => $subquery));
      //to debug



      $result = $proceed($collection);

      if ($currentOrder) {
      if ($currentOrder == 'price_high') {
      $subject->getCollection()->setOrder('price_sort', ' DESC');
      # $subject->getSelect()->order('price_sort DESC');
      } elseif ($currentOrder == 'price_low') {
      $subject->getCollection()->setOrder('price_sort', 'asc');
      }elseif ($currentOrder == 'price_low') {
      $subject->getCollection()->setOrder('position', 'desc');
      }elseif ($currentOrder == 'bestseller') {
      $subject->getCollection()->setOrder('price', 'asc');
      }
      }

      return $result;
      }

      public function aftergetAvailableOrders()
      {
      $sortFilter=array(
      'price_high'=>__('Price High To Low'),
      'price_low'=>__('Price Low To High'),
      'position'=>__('Newlastest'),
      'bestseller'=>__('Bestseller'),

      );
      return $sortFilter;
      }
      }


      In product/list.phtml, I debug query but in order by it's not showing.



      I want to show my query <query sql> order by price_sort DESC



      But if in toolbar.php, I replace



      $subject->getCollection()->setOrder('price_sort', ' DESC');


      to



      $subject->getCollection()->setOrder('price', ' DESC');


      and then I debug the query, then in query it shows



      order by price DESC



      I want use column price_sort in setOrder.







      magento2 collection custom






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 1 '17 at 6:24









      Teja Bhagavan Kollepara

      2,97341847




      2,97341847










      asked Sep 15 '17 at 2:15









      Jerry NguyenJerry Nguyen

      283




      283





      bumped to the homepage by Community 1 min ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 1 min ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Whenever you try to use setOrder for a custom column it will take no effect.



          $subject->getCollection()->setOrder('price_sort', ' DESC');


          You instead need to access the select directly



          $subject->getCollection()->getSelect()->order('price_sort DESC');


          It should be noted that a side effect of adding order queries this way will be placed before all order queries placed using the setOrder method.






          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%2f193309%2fhow-set-order-with-custom-field-column-magento-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Whenever you try to use setOrder for a custom column it will take no effect.



            $subject->getCollection()->setOrder('price_sort', ' DESC');


            You instead need to access the select directly



            $subject->getCollection()->getSelect()->order('price_sort DESC');


            It should be noted that a side effect of adding order queries this way will be placed before all order queries placed using the setOrder method.






            share|improve this answer




























              0














              Whenever you try to use setOrder for a custom column it will take no effect.



              $subject->getCollection()->setOrder('price_sort', ' DESC');


              You instead need to access the select directly



              $subject->getCollection()->getSelect()->order('price_sort DESC');


              It should be noted that a side effect of adding order queries this way will be placed before all order queries placed using the setOrder method.






              share|improve this answer


























                0












                0








                0







                Whenever you try to use setOrder for a custom column it will take no effect.



                $subject->getCollection()->setOrder('price_sort', ' DESC');


                You instead need to access the select directly



                $subject->getCollection()->getSelect()->order('price_sort DESC');


                It should be noted that a side effect of adding order queries this way will be placed before all order queries placed using the setOrder method.






                share|improve this answer













                Whenever you try to use setOrder for a custom column it will take no effect.



                $subject->getCollection()->setOrder('price_sort', ' DESC');


                You instead need to access the select directly



                $subject->getCollection()->getSelect()->order('price_sort DESC');


                It should be noted that a side effect of adding order queries this way will be placed before all order queries placed using the setOrder method.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 1 '17 at 4:25









                TyphadoTyphado

                346417




                346417






























                    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%2f193309%2fhow-set-order-with-custom-field-column-magento-2%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