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
    Jun 7 '20 at 20:22
  • @paj, Can you explain in brief, i am not getting it.
    – Manjunath
    Jun 8 '20 at 5:44
  • i am not getting it. where to add your code there?
    – Manjunath
    Jun 8 '20 at 5:51
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
    Jun 12 '20 at 13:25
  • You will have to specify each attribute you want to sort and set the sortbyoptiontitle variable to true
    – paj
    Jun 12 '20 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
    Jun 15 '20 at 4:42
  • hi @paj, I have options 0-6months, 6-12months, 12-24months, which is not sorting properly
    – Manjunath
    Jun 15 '20 at 12:14
  • hello @paj, can you please update me for this
    – Manjunath
    Jun 15 '20 at 15:37

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.