0

I want to sort products by regular not final price or special price. any idea how to do that Here you can see special price and regualr price. enter image description here

and this is my code.

Vendor\module\Model\VisualMerchandiser\Sorting\OriginalPriceTop.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\module\Model\VisualMerchandiser\Sorting;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Data\Collection as CollectionAlias;
use Magento\VisualMerchandiser\Model\Sorting\SortAbstract;
use Magento\VisualMerchandiser\Model\Sorting\SortInterface;
use Zend_Db_Select;

class OriginalPriceTop extends SortAbstract implements SortInterface
{
    /**
     * @param Collection $collection
     * @return Collection
     */
    public function sort(
        Collection $collection
    ): Collection {
        $this->addPriceData($collection);
        $collection->getSelect()
            ->distinct('entity_id')
            ->reset(Zend_Db_Select::ORDER)
            ->order('price ' . CollectionAlias::SORT_ORDER_DESC);

        return $collection;
    }

    /**
     * @return string
     */
    public function getLabel(): string
    {
        return __("Original Price Top");
    }
}

Is this code good or its needs to change?

1 Answer 1

1

Oh man, again question from you about the same topic and without any answers

This code should work and I think here is everything is OK

I just can suggest to use correlation table for price field for prevent any ambiguous cases. Just change order('price ' to order('price_index.price '

Good luck and I hope you already received some experience to write Magento things

2
  • Thanks, i'm still learning, long way to go
    – P.A
    Aug 7 at 8:22
  • the client wants 30 custom sorting options, 10 were already implemented, 15 were implemented with your help and still 5 remaining. so....
    – P.A
    Aug 7 at 8:25

Your Answer

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

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