0

So I have a bit of an issue. I am trying to create a custom sort order in the magneto 2 catalogue pages using a calculation between two price fields.

I am currently editing Magento\Catalog\Block\Product\ProductList\Toolbar::setCollection()

but will use a plugin when I figured out my problem

This is where my problem lies. I cannot use setOrder() as there is no real field and I cannot seem to use addExpressionAttributeToSelect() to get around that as it shouts about the price the fields already being joined.

So I thought Id use $collection->getSelect()->order(); But no matter what I do it does not seem to work. Even if I use a basic $collection->getSelect()->order('price') (or price_index.price) it is entirely ignored.

I know my queries do actually work if I print them out via $collection->getSelect()__toString() they work as expected when run in mysql

Is there a best practice way that I can calculate the the difference between 2 price fields and have my collection actually order by it in this section of the site? Am I missing something really obvious?

Example

if ($this->getCurrentOrder()) {
    if (($this->getCurrentOrder()) === 'position') {
        $this->_collection->addAttributeToSort(
            $this->getCurrentOrder(),
            $this->getCurrentDirection()
        );
    } else if ($this->getCurrentOrder() === 'custom') {
$this->_collection->setOrder('price') //this works
//$this->_collection->getSelect()->order('price_index.price'); // this doesnt

    } else {
        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }
}
return $this;

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.