9

The order of the dropdown is not consistent with the order of the attribute itself. Within the attribute size I have it ordered Newborn 0-3 mo

and in the dropdown it shows up as 0-3 Newborn

the size Newborn was added a few days after the other sizes and the newborn simple products were imported after the 0-3 mo. Any suggestions of how to get the order of the drop down to follow the order of the list within the attribute? I have cleared cache and reindexed. I read that @Khoa TruongDinh had the same issue but did not see a solution to the issue so am reposting.

  • also, the attribute products with size 0-3 already exist and I am adding all the attribute products with size Newborn. – babywit Oct 20 '16 at 18:13
14

I faced the same attribute options sorting issue at front-end, As I checked this issue and found that while fetching the attribute options, there is no sorting filter added in the query by default in Magento 2.1.2, So to fix this issue need to add the below code to add the ORDER By in function getAttributeOptions on line no 282 in file: vendor/magento/module-configurable-product/Model/ResourceModel/Product/Type/Configurable.php Now, it's working fine for me.

->joinInner(
        ['attribute_opt' => $this->getTable('eav_attribute_option')],
        'attribute_opt.option_id = entity_value.value',
        []
    )->order(
        'attribute_opt.sort_order ASC'
    );

If unable to edit the code then please replace this getAttributeOptions function with code as below:

public function getAttributeOptions($superAttribute, $productId)
{
    $scope  = $this->getScopeResolver()->getScope();
    $select = $this->getConnection()->select()->from(
        ['super_attribute' => $this->getTable('catalog_product_super_attribute')],
        [
            'sku' => 'entity.sku',
            'product_id' => 'product_entity.entity_id',
            'attribute_code' => 'attribute.attribute_code',
            'value_index' => 'entity_value.value',
            'option_title' => $this->getConnection()->getIfNullSql(
                'option_value.value',
                'default_option_value.value'
            ),
            'default_title' => 'default_option_value.value',
        ]
    )->joinInner(
        ['product_entity' => $this->getTable('catalog_product_entity')],
        "product_entity.{$this->getProductEntityLinkField()} = super_attribute.product_id",
        []
    )->joinInner(
        ['product_link' => $this->getTable('catalog_product_super_link')],
        'product_link.parent_id = super_attribute.product_id',
        []
    )->joinInner(
        ['attribute' => $this->getTable('eav_attribute')],
        'attribute.attribute_id = super_attribute.attribute_id',
        []
    )->joinInner(
        ['entity' => $this->getTable('catalog_product_entity')],
        'entity.entity_id = product_link.product_id',
        []
    )->joinInner(
        ['entity_value' => $superAttribute->getBackendTable()],
        implode(
            ' AND ',
            [
                'entity_value.attribute_id = super_attribute.attribute_id',
                'entity_value.store_id = 0',
                "entity_value.{$this->getProductEntityLinkField()} = "
                . "entity.{$this->getProductEntityLinkField()}",
            ]
        ),
        []
    )->joinLeft(
        ['option_value' => $this->getTable('eav_attribute_option_value')],
        implode(
            ' AND ',
            [
                'option_value.option_id = entity_value.value',
                'option_value.store_id = ' . $scope->getId(),
            ]
        ),
        []
    )->joinLeft(
        ['default_option_value' => $this->getTable('eav_attribute_option_value')],
        implode(
            ' AND ',
            [
                'default_option_value.option_id = entity_value.value',
                'default_option_value.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ]
        ),
        []
    )->where(
        'super_attribute.product_id = ?',
        $productId
    )->where(
        'attribute.attribute_id = ?',
        $superAttribute->getAttributeId()
    )->joinInner(
        ['attribute_opt' => $this->getTable('eav_attribute_option')],
        'attribute_opt.option_id = entity_value.value',
        []
    )->order(
        'attribute_opt.sort_order ASC'
    );

    return $this->getConnection()->fetchAll($select);
}
| improve this answer | |
  • thank you so much for this but I am not well versed in php. I am not sure where to add in your fix. This is what I have from 280-282 and tried to add it in before the ; on 282 but this did not work. 280 'attribute.attribute_id = ?', 281 $superAttribute->getAttributeId() 282 ); – babywit Nov 22 '16 at 7:09
  • if you are unable to edit code then please replace this – Jagdish Ram Nov 22 '16 at 9:39
  • Has anybody written an extension that applies this patch? – TheNorthern_Light Oct 23 '17 at 18:26
  • Also, this doesn't seem to work in 2.1.9. – TheNorthern_Light Oct 23 '17 at 18:33
  • In 2.1.14 the functionality has moved to Magento\ConfigurableProduct\Model\AttributeOptionProvider. Looks solved at first glance, not sure about possible bugs though. – simonthesorcerer May 2 '19 at 14:29
3
  1. Go to Configurable product page -> Edit Configuration -> Click next -next - next and don't change anything
  2. Then save the product and they should be in order.
| improve this answer | |
  • Refresh or Save the product ? – Lasantha May 23 '18 at 5:47
  • How to do that with 300+ products? – Mohammed Joraid Jun 24 '18 at 5:51
  • Actually only opening the configurable product edit page, and then just saving directly fixes the sort order. So 1- Open the product 2- Click save. But how to make this work without going to all products manually and save them. I tried to bulk add all product to Site hoping it will trigger a save event but the sort was not fixed. using MAG2.2.2 – Mohammed Joraid Jun 24 '18 at 17:31
2

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;
}
| improve this answer | |
  • can you elaborate it more? – Kowsigan Atsayam Mar 13 at 16:40
  • hi @paj, where we have to add this code in that file? – Manjunath Jun 8 at 6:36
  • @Manjunath add the sort function to the getAttributeOptions method before $data is returned, I have updated my answer. – paj Jun 8 at 14:05
  • @paj, Thanks, can we sort alphabatically only for specific attribute ex: only for color i have to sort alphabetic but not for remaining attributes, the above code sorting for all the attributes – Manjunath Jun 12 at 8:24
  • @Manjunath see my updated answer to just sort a specific attribute i.e. color. – paj Jun 12 at 13:04
1

If you mean that Newborn is attribute value, you need to go to Stores -> Attributes (Product), find needed attribute and also using drag&drop with your mouse change position of options. enter image description here

The position of drop-downs themselves (size, color, shape) can be set when generating associated products. Open edit form -> Advanced settings -> Edit configurations - Attribute values step and with the help of drag&drop move attributes enter image description here

| improve this answer | |
  • Yes exactly. "Within the attribute size I have it ordered Newborn 0-3 mo" So inside the attribute itself Newborn is ABOVE 0-3 mo and in the product dropdown it shows up with 0-3 mo ABOVE Newborn. – babywit Oct 21 '16 at 19:23
  • So as far as I can tell, the ordering within the attribute itself is not working properly in magento 2.1.2 – babywit Oct 21 '16 at 19:24
  • Anyone have any idea what is causing this issue with the order of the attributes not showing up in the order listed in the attribute or a workaround to get the attributes to show up in the correct order? I want to launch my store but this is driving me batty. – babywit Nov 5 '16 at 18:23
1

This is currently a known issue in Magento 2. It is still an issue as of version 2.1.4.

Here is the GitHub issue: https://github.com/magento/magento2/issues/7441

| improve this answer | |
  • The question was asked 3 months ago. Magento 2.1.4 was released yesterday... – 7ochem Feb 8 '17 at 20:39
1

Because this issue is still present also in the latest release 2.1.7 you can use this workaround:

Go to configurable product page->Configurations->remove all simple products

After that add them again in the desired order:

Add products manually->Filter products by name->Add simple products in the desired order.

| improve this answer | |

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.