0

I am facing problem in product list sorting by product name if the name is alphanumeric, Such as "Facewash, 50ml", "Facewash, 200ml" and "Facewash, 500ml".

Expected result:- "Facewash,50ml", "Facewash,200ml" and "Facewash, 500ml".

Actual Result:- "Facewash,200ml", "Facewash, 5ooml", "Facewash,50ml".

Below are the screenshots of configuration settings and the frontend list page.

Configuration Setting

Product List page.

Product list page

Please suggest what I suppose to do.

1 Answer 1

0

You need to add one more custom attribute for ordering or adding integer values to make order proper.

ie. you need to add ml in custom field in integer value.

then you can create plugin for modify collection data set by using plugin.

Here is an example.

app\code\vendor\extension\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    
    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="catalog_productlist_toolbar_plugin" type="Vendor\Extension\Plugin\Product\ProductList\Toolbar"/>
    </type>
</config>

app\code\Vendor\Extension\Plugin\Product\ProductList\Toolbar.php

<?php
namespace Vendor\Extension\Plugin\Product\ProductList;
class Toolbar
{
    public function aroundSetCollection(
        \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
        \Closure $proceed,
        $collection
    ) {
        $currentOrder = $subject->getCurrentOrder();
        if ($currentOrder == "name") {
            $dir = $subject->getCurrentDirection();
            $collection->getSelect()->addAttributeToSort('your_attribute', $dir)->order('name '.$dir); // you can add filter as per your requirement.
        }
        return $proceed($collection);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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