0

I am trying to sort show the attribute option in alphabetically for the configurable products.

Followed below answer.

Configurable product attribute sort order in Dropdown 2.12

But in 2.3.4 the code is not like in the suggested answer, Can anyone help me with how can i show options in alphabetic order. Thanks in advance!!

3
  • my answer to the question works in 2.3.x
    – paj
    Commented Jun 7, 2020 at 20:22
  • @paj, Can you explain in brief, i am not getting it.
    – Manjunath
    Commented Jun 8, 2020 at 5:44
  • i am not getting it. where to add your code there?
    – Manjunath
    Commented Jun 8, 2020 at 5:51

3 Answers 3

1

try to override the file Magento\ConfigurableProduct\Model\AttributeOptionProvider.php

in your module and put below code before the return in the function getAttributeOptions.

usort($data, function($a, $b) {
        return $a['option_title'] <=> $b['option_title'];
});

I hope this will help you!!

1

In v2.3.x you can sort the order of the attributes by option label in a configurable product drop down selector by extending

Magento\ConfigurableProduct\Model\AttributeOptionProvider

and using

usort($data, function($a, $b) {
    return $a['option_title'] <=> $b['option_title'];
});

To sort the returned option data array $data in method getAttributeOptions() for a specific attribute i.e. color use

/**
* {@inheritdoc}
*/
public function getAttributeOptions(AbstractAttribute $superAttribute, $productId)
{
   $scope  = $this->scopeResolver->getScope();
   $select = $this->optionSelectBuilder->getSelect($superAttribute, $productId, $scope);
   $data = $this->attributeResource->getConnection()->fetchAll($select);

   $sortByOptionTitle=false;

   if ($superAttribute->getSourceModel()) {
       $options = $superAttribute->getSource()->getAllOptions(false);

       $optionLabels = [];
       foreach ($options as $option) {
           $optionLabels[$option['value']] = $option['label'];
       }

       foreach ($data as $key => $value) {
           $optionText = isset($optionLabels[$value['value_index']])
               ? $optionLabels[$value['value_index']]
               : false;
           $data[$key]['default_title'] = $optionText;
           $data[$key]['option_title'] = $optionText;

           if (isset($data[$key]['attribute_code']))
           {
               if ($data[$key]['attribute_code']=='color')
               {
                   $sortByOptionTitle=true;
               }
           }
       }

       if ($sortByOptionTitle)
       {
           usort($data, function($a, $b) {
               return $a['option_title'] <=> $b['option_title'];
           });
       }
   }

   return $data;
}
8
  • hi @paj, Thank you for your answer, I have options like, 1-6months, 6-12months, 12-24months, which is not sorting alphabetically, can you please update on this
    – Manjunath
    Commented Jun 12, 2020 at 13:25
  • You will have to specify each attribute you want to sort and set the sortbyoptiontitle variable to true
    – paj
    Commented Jun 12, 2020 at 14:55
  • hi @paj, yes i can give that, but i have options like above , which is not sorting, as we have numbers in the string
    – Manjunath
    Commented Jun 15, 2020 at 4:42
  • hi @paj, I have options 0-6months, 6-12months, 12-24months, which is not sorting properly
    – Manjunath
    Commented Jun 15, 2020 at 12:14
  • hello @paj, can you please update me for this
    – Manjunath
    Commented Jun 15, 2020 at 15:37
0
  1. Create di.xml in the etc folder
  2. Add preference
  <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <preference for="Magento\ConfigurableProduct\Model\AttributeOptionProvider" type="Vendor\ModuleName\Model\AttributeOptionProvider"/> 
  </config>
  1. Create Model AttributeOptionProvider.php
<?php
namespace I95Dev\ProductOptionSort\Model;
use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\App\ScopeResolverInterface;
use Magento\Framework\DB\Select;

class AttributeOptionProvider extends \Magento\ConfigurableProduct\Model\AttributeOptionProvider
{

        private $scopeResolver;

        private $attributeResource;
    
        private $optionSelectBuilder;
    
        public function __construct(
            Attribute $attributeResource,
            ScopeResolverInterface $scopeResolver,
            OptionSelectBuilderInterface $optionSelectBuilder
        ) {
            $this->attributeResource = $attributeResource;
            $this->scopeResolver = $scopeResolver;
            $this->optionSelectBuilder = $optionSelectBuilder;
        }
    
        public function getAttributeOptions(AbstractAttribute $superAttribute, $productId)
        {
            $scope = $this->scopeResolver->getScope();
            $select = $this->optionSelectBuilder->getSelect($superAttribute, $productId, $scope);
            $data = $this->attributeResource->getConnection()->fetchAll($select);
    
            $sortByOptionTitle = false;
    
            if ($superAttribute->getSourceModel()) {
                $options = $superAttribute->getSource()->getAllOptions(false);
    
                $optionLabels = [];
                foreach ($options as $option) {
                    $optionLabels[$option['value']] = $option['label'];
                }
    
                foreach ($data as $key => $value) {
                    $optionText = isset($optionLabels[$value['value_index']]) ? $optionLabels[$value['value_index']] : false;
                    $data[$key]['default_title'] = $optionText;
                    $data[$key]['option_title'] = $optionText;
    
                    if (isset($data[$key]['attribute_code'])) {
                        if ($data[$key]['attribute_code'] == $data[$key]['attribute_code']) {
                            $sortByOptionTitle = true;
                        }
                    }
                }
                if ($sortByOptionTitle) {
                    usort($data, function ($a, $b) {
                        return $a['option_title'] <=> $b['option_title'];
                    });
                }
            }
            return $data;
        }
    }

This will arrange product options alphabetically (Kudos)

Your Answer

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

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