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!!

有帮助吗?

解决方案

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!!

其他提示

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;
}
许可以下: CC-BY-SA归因
scroll top